From a76b9df329ceb3a679e837ed7c92492e03b4139b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 4 Sep 2022 18:17:06 +0100 Subject: [PATCH] Fix 404s if there's no resolver match --- website/common/tests/test_views.py | 6 ++++++ website/common/views.py | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/website/common/tests/test_views.py b/website/common/tests/test_views.py index 51c8842..847b07c 100644 --- a/website/common/tests/test_views.py +++ b/website/common/tests/test_views.py @@ -15,6 +15,12 @@ class Error404PageTestCase(TestCase): response, "

There's nothing here!

", html=True, status_code=404 ) + def test_actual_404_no_url_match(self) -> None: + response = self.client.get("/favicon.ico") + self.assertContains( + response, "

There's nothing here!

", html=True, status_code=404 + ) + def test_queries(self) -> None: with self.assertNumQueries(20): self.client.get(self.url) diff --git a/website/common/views.py b/website/common/views.py index b077793..1755781 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -19,7 +19,8 @@ class Error404View(TemplateView): template_name = ERROR_404_TEMPLATE_NAME def render_to_response(self, context: dict, **response_kwargs: Any) -> HttpResponse: - if self.request.resolver_match.url_name != "404": + resolver_match = self.request.resolver_match + if not resolver_match or resolver_match.url_name != "404": response_kwargs["status"] = 404 return super().render_to_response(context, **response_kwargs)