Hi all,
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.
Any suggestion?
Update:
I've just founded an elegant and working solution to do this.
According to the Django Documentation:
The 404 view is passed a
RequestContext
and will have access to variables supplied by yourTEMPLATE_CONTEXT_PROCESSORS
setting (e.g.,MEDIA_URL
).
So you just need to write your own Template Context Processor:
def extra_var(request):
return {'my_extra_var': 'my_extra_var'}
and add it to the TEMPLATE_CONTEXT_PROCESSORS
tuple