Fix 404s if there's no resolver match

This commit is contained in:
Jake Howard 2022-09-04 18:17:06 +01:00
parent f40681d06a
commit a76b9df329
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 8 additions and 1 deletions

View file

@ -15,6 +15,12 @@ class Error404PageTestCase(TestCase):
response, "<h1>There's nothing here!</h1>", html=True, status_code=404
)
def test_actual_404_no_url_match(self) -> None:
response = self.client.get("/favicon.ico")
self.assertContains(
response, "<h1>There's nothing here!</h1>", html=True, status_code=404
)
def test_queries(self) -> None:
with self.assertNumQueries(20):
self.client.get(self.url)

View file

@ -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)