Redirect any urls to 404.html if not found in urls.py in django

Ankan Kumar Giri picture Ankan Kumar Giri · May 14, 2015 · Viewed 15.7k times · Source

How can I redirect any kind of url patterns to a created page "404.html" page if it doesn't exist in the urls.py rather than being shown the error by django.

Answer

moonstruck picture moonstruck · May 14, 2015

Make a view that'll render your created 404.html and set it as handler404 in urls.py.

handler404 = 'app.views.404_view'

Django will render debug view if debug is enabled. Else it'll render 404 page as specified in handler404 for all types of pages if it doesn't exist.

Django documentation on Customizing error views.

Check this answer for a complete example.