Skip to content

Commit 6cacb56

Browse files
committed
feat: implement a page not found (404) error view and template
1 parent 9876bfc commit 6cacb56

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

pems/core/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.views.generic import TemplateView
2+
3+
4+
class PageNotFoundView(TemplateView):
5+
template_name = "404.html"
6+
7+
def dispatch(self, request, *args, **kwargs):
8+
response = super().dispatch(request, *args, **kwargs)
9+
response.status_code = 404
10+
return response

pems/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _filter_empty(ls):
5252
TEMPLATES = [
5353
{
5454
"BACKEND": "django.template.backends.django.DjangoTemplates",
55-
"DIRS": [],
55+
"DIRS": [os.path.join(BASE_DIR, "pems", "templates")],
5656
"APP_DIRS": True,
5757
"OPTIONS": {
5858
"context_processors": [

pems/templates/404.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "core/base.html" %}
2+
{% block headline %}
3+
Sorry! We can’t find that page.
4+
{% endblock headline %}
5+
{% block inner-content %}
6+
<p>The page you are looking for might be somewhere else or may not exist anymore.</p>
7+
{% endblock inner-content %}

pems/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from django.contrib import admin
22
from django.urls import include, path
33

4+
from pems.core.views import PageNotFoundView
5+
6+
7+
handler404 = PageNotFoundView.as_view()
8+
49
urlpatterns = [
510
path("", include("pems.core.urls")),
611
path("admin/", admin.site.urls),

tests/pytest/pems/core/test_views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from pems.core import views
4+
5+
6+
class TestPageNotFoundView:
7+
@pytest.fixture
8+
def view(app_request):
9+
v = views.PageNotFoundView()
10+
v.setup(app_request)
11+
12+
return v
13+
14+
def test_dispatch(self, view, app_request):
15+
16+
response = view.dispatch(app_request)
17+
18+
assert response.status_code == 404

0 commit comments

Comments
 (0)