Django: custom error pages

May 10, 2015

Last update: March 28, 2020

Tested with Django 1.9 and newer

The problem

Yes. Shit happens and our application must be ready to manage correclty exceptions and give always a feedback to our users. Well designed error pages have the same layout of all other site pages. This means avoiding that our users saw the dafault Nginx/Apache error pages having the feeling that they had quitted our application.

A solution

Django offers a simple and elegant way to manage the more common HTTP errors, such as 400, 403, 404 and 500, enabling us to specify our custom views and templates.

All we need is to add these lines in our


from django.conf.urls import (
    handler400, handler403, handler404, handler500

handler400 = 'my_app.views.bad_request'
handler403 = 'my_app.views.permission_denied'
handler404 = 'my_app.views.page_not_found'
handler500 = 'my_app.views.server_error'

# ...

and implement our custom views in


from django.shortcuts import (
from django.template import RequestContext

# HTTP Error 400
def bad_request(request):
    response = render_to_response(
    response.status_code = 400
    return response

# ...


  1. Customizing error views

A photo of Elia Contini
Written by
Elia Contini
Sardinian UX engineer and a Front-end web architect based in Ticino, Switzerland. Marathoner, traveller, wannabe nature photographer.