So I've set up a path in my URL config:
path(
"kitten/",
views.Kitten.as_view(),
name="kitten",
),
and a handler for missing URLs to the same view.
handler404 = views.Kitten.as_view()
I have some middleware which sets some context data:
class CookieConsentMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_template_response(self, request, response):
response.context_data["hasCookie"] = False
return response
and the view is very straightforward,
class Kitten(TemplateView):
template_name = "kitten.html"
and the template prints the value of hasCookie.
Visiting kitten/ correctly displays the value of hasCookie, but visiting a URL that doesn't exist, displays no value for hasCookie (whilst showing the right template)
Adding debugging statements to the middleware, it becomes apparent that whilst process_view, process_template_response and process_exception are called for kitten/, none of these are called for URLs that don't exist, so no code is called to set the value of hasCookie. (__init__ is called regardless at app startup)
Why does it not call the middleware when the URL is not found in the URLconf?