Skip to content

Commit

Permalink
feat(error-handling): custom 404 handler
Browse files Browse the repository at this point in the history
- Create a custom 404 Not Found page and improve the handler404 with a better 404.html template
- Add comments, formatting, and pylint suppressions to satisfy pylint
- Apply Black formatting
- Remove unnecessary DEBUG line

Refs: #100, PR#102
  • Loading branch information
zhannaklimanova committed Jun 18, 2024
1 parent e846521 commit 817f297
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
21 changes: 13 additions & 8 deletions web-app/django/VIM/templates/404.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{% extends "base.html" %}

{% block title %}
Oops
{% endblock %}

{% block content %}
<h1>Oops</h1>
<h2>We can't seem to find the page you are looking for.</h2>
{% endblock%}
<div class="container">
<div class="row">
<div class="p-3 col-12">
<h3>404</h3>
<h5>Page not found</h5>
<br>
<p>
We couldn't find a page at this address.
</p>
</div>
</div>
</div>
{% endblock %}
9 changes: 9 additions & 0 deletions web-app/django/VIM/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@

if settings.IS_DEVELOPMENT:
urlpatterns += [path("__debug__/", include("debug_toolbar.urls"))]


###############################################################
# Custom error handlers #
# These handlers define custom views to be displayed for #
# specific HTTP error responses. #
###############################################################
# pylint: disable=invalid-name
handler404 = "VIM.views.custom_404_page_not_found"
23 changes: 23 additions & 0 deletions web-app/django/VIM/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Module for custom error views.
"""

from django.http import HttpRequest, HttpResponse
from django.shortcuts import render


# pylint: disable=unused-argument
def custom_404_page_not_found(
request: HttpRequest, exception: Exception
) -> HttpResponse:
"""
Custom view to handle 404 errors.
Args:
request (HttpRequest): The request object.
exception (Exception): The exception that triggered the 404 error.
Returns:
HttpResponse: Rendered 404 error page.
"""
return render(request, "404.html", status=404)

0 comments on commit 817f297

Please sign in to comment.