Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing code for a custom Django 404 page

Tags:

python

django

I am getting ready to deploy my first Django application and am hitting a bit of a roadblock. My base template relies on me passing in the session object so that it can read out the currently logged in user's name. This isn't a problem when I control the code that is calling a template.

However, as part of getting this app ready to be deployed, I need to create a 404.html page. I extended my base template just like I've done with my other pages, but I don't see a way to pass in the session object so that I can utilize it. Is there a way to have Django call a custom method to render your 404 rather than just rendering your 404.html for you?

like image 931
Chris Lieb Avatar asked Jul 22 '09 18:07

Chris Lieb


2 Answers

You need to override the default view handler for the 404 error. Here is the documentation on how to create your own custom 404 view function:

http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

like image 111
AlbertoPL Avatar answered Oct 23 '22 15:10

AlbertoPL


Define your own 404 handler. See Django URLs, specifically the part about handler404.

like image 32
Alexander Ljungberg Avatar answered Oct 23 '22 14:10

Alexander Ljungberg