I want to be able to use extra variables on a custom 404 template.
#404.html
{{ extra_var }}
I have already tried:
#urls.py
from myproject.myapp import views
handler404 = views.handler404
#views.py
from django.template import RequestContext, loader
from django import http
def handler404(request):
extra_var = 'my_extra_var'
t = loader.get_template('404.html')
return http.HttpResponseNotFound(t.render(RequestContext(request,
{'request_path': request.path, 'extra_var': extra_var, })))
However, it doesn’t seem to work: I can only access to request_path.
The fact that you can access
request_pathbut notextra_varsuggests to me your view is not being called properly, sincerequest_pathis passed automatically to the404.htmltemplate, per the documentation:I think you need to give
handler404a string, rather than a module, like this: