From 61208606f7d7046efed69ef06275d1bf13568c8d Mon Sep 17 00:00:00 2001 From: catborise Date: Sat, 3 Oct 2026 10:23:53 +0300 Subject: [PATCH] fix: render the 404 page for logged-in users app_active and view_active read request.resolver_match, which is None when no URL matched, so the navbar raised AttributeError and the 404 became a 500. --- webvirtcloud/common_tags.py | 7 +++++-- webvirtcloud/test_common_tags.py | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 webvirtcloud/test_common_tags.py diff --git a/webvirtcloud/common_tags.py b/webvirtcloud/common_tags.py index 4b85df72..d9f97044 100644 --- a/webvirtcloud/common_tags.py +++ b/webvirtcloud/common_tags.py @@ -7,12 +7,15 @@ register = template.Library() @register.simple_tag def app_active(request, app_name): - return "active" if request.resolver_match.app_name == app_name else "" + # resolver_match is None on error pages such as 404 + match = request.resolver_match + return "active" if match and match.app_name == app_name else "" @register.simple_tag def view_active(request, view_name): - return "active" if request.resolver_match.view_name == view_name else "" + match = request.resolver_match + return "active" if match and match.view_name == view_name else "" @register.simple_tag diff --git a/webvirtcloud/test_common_tags.py b/webvirtcloud/test_common_tags.py new file mode 100644 index 00000000..029d4635 --- /dev/null +++ b/webvirtcloud/test_common_tags.py @@ -0,0 +1,9 @@ +from django.contrib.auth import get_user_model +from django.test import TestCase + + +class NotFoundPageTestCase(TestCase): + def test_unknown_url_renders_404_for_logged_in_user(self): + user = get_user_model().objects.create_user("tags_user", "t@example.com", "pw") + self.client.force_login(user) + self.assertEqual(self.client.get("/no-such-page/").status_code, 404)