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

In your views.py, just add the following code (No need to change anything in urls.py).

from django.shortcuts import render_to_response
from django.template import RequestContext


def handler404(request):
    response = render_to_response('404.html', {},
                              context_instance=RequestContext(request))
    response.status_code = 404
    return response

Put a custom 404.html in templates directory.

source : click here


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.


Go to your project settings.py and set DEBUG = True to DEBUG = False Then Django redirects all NOT set patterns to not found.

In additional if you want to customize 404 template , in your project urls.py

set

handler404 = 'app.views.404_view'

then in your projects view.py

from django.shortcuts import render_to_response
from django.template import RequestContext

def handler404(request):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response

and Finally, in your templates add 404.html and fill it with what you want to show end user.


There is no need to change anything in your view or url. Just do these 2 steps, in your settings.py, do the following

DEBUG = False
ALLOWED_HOSTS = ["*"]

And in your app directory (myapp in this example), create myapp/templates/404.html where 404.html is your custom error page. That is it.