Search code examples
pythonregexdjangodjango-urls

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


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.


Solution

  • 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.