Fix crash when the current view cannot be determined.
authorSybren A. Stüvel <sybren@stuvel.eu>
Thu, 14 Feb 2019 09:40:36 +0000 (10:40 +0100)
committerSybren A. Stüvel <sybren@stuvel.eu>
Thu, 14 Feb 2019 09:40:36 +0000 (10:40 +0100)
The view being undeterminable could be caused by other problems, but at
least now we don't add to those.

blender_fund_main/context_processors.py

index 665b5b2..390b75d 100644 (file)
@@ -2,6 +2,8 @@
 Makes 'settings' available as template variable.
 """
 
+import typing
+
 import django.conf
 from django.http import HttpRequest
 from django.urls import ResolverMatch
@@ -16,8 +18,13 @@ def page_id(request: HttpRequest) -> dict:
 
     When using class-based views, set the `page_id` attribute on the class.
     When using function-based views, set the `page_id` attribute on the function.
+
+    When the current view cannot be determined, {'page_id': ''} is returned.
     """
-    resolver_match: ResolverMatch = request.resolver_match
+    resolver_match: typing.Optional[ResolverMatch] = request.resolver_match
+    if resolver_match is None:
+        return {'page_id': ''}
+
     view_func = resolver_match.func
 
     try: