Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different 404 pages depending on the application in Django

Tags:

python

django

We are developing a project with several applications using Django. It shares the database, but it has several applications targeting different very different users. Roughly, administrators and final users. The UI of each application is very different. I need to create a 404 error page, but seems that I can only create one for all the project. I would like to create different 404 templates and being able to shown them depending on the application (URL) the user is asking for...

For general, clearly invalid URL it's easy, but in the code there are other ways of launching exceptions, like get_object_or_404 calls.

Anyone knows a way of doing that?

like image 586
Khelben Avatar asked Feb 22 '10 16:02

Khelben


People also ask

How do I show 404 in Django?

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.


2 Answers

It's not at all true that you can only create a single 404 page for the entire app. The documentation explains how you can create a specific 404 handler view, which of course can look at the value of request.path to see what URL was requested and render the relevant template.

like image 127
Daniel Roseman Avatar answered Oct 27 '22 01:10

Daniel Roseman


Write a 404 view by setting handler404, and not just a template. In that view, try to figure from the url which 404 you should show, and render that.

like image 40
Ofri Raviv Avatar answered Oct 26 '22 23:10

Ofri Raviv