Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
Designing custom 404 and 500 error pages in Django

Update 1: Please refer this updated article for Django 2.0 and source code.  

It happens very frequently that a visitor on your website typed a wrong URL or the page user is looking for no longer exists.

What do you do to handle such cases. You have three options.

  • Redirect the visitor to home page, silently.
  • Show a boring 404 page and then ask them to click on a link.
  • Create your own funny/awesome/informative custom 404 error page.

In this article we will discuss the third option.

How to create your own custom 404 error page in Django:

A custom 404 error page can serve multiple other purposes apart from just telling the user that the link you visited is not correct.

You can ask user to subscribe or sign-up. Or you may show some funny stuff.

custom 404 error page in django

Default 404 error page in django is quite boring. Also creating a custom 404 page in django is very simple.  

So lets see how to create custom 404 error page in django.

  • In  file of your project, import handler404  and handler505 .
  • In  file import views from your app.
  • After urlpatterns assign the views to handler404 and handler500.

    from myapp import views as myapp_views
    from django.conf.urls import handler404, handler500
    urlpatterns = [
        url(r'^myapp/', include('myapp.urls', namespace='myapp')),
    handler404 = myapp_views.error_404
    handler500 = myapp_views.error_500

  • In file, set DEBUG=False  and ALLOWED_HOST=["*"] . Custom 404 and 500 pages works only when Debug is set to false and there is appropriate entry in allowed_hosts .
  • Create a new view function in your views file. Return the rendered 404 html file from this view. Make sure name of view function created in this file matches the name used in urls file above.

    from django.shortcuts import render
    def error_404(request):
            data = {}
            return render(request,'myapp/error_404.html', data)
    def error_500(request):
            data = {}
            return render(request,'myapp/error_500.html', data)

  • Now create an HTML template in your app's template directory myapp/templates/myapp/error_404.html . Place whatever content you want to put there. You can create a subscribe form or sign up form.
  • Now type an incorrect url and you will be shown your custom 404 page. In case of any internal server error like syntax error in some template, custom 500 page will be shown.

  Comment in case of any query.
2 comments on 'Designing Custom 404 And 500 Error Pages In Django'
Login to comment

David Ogutu Feb. 18, 2019, 10:35 p.m.
You should just use the default error page templates e.g. 404.html on your root template dir:
Ryan Denton March 29, 2023, 6:39 a.m.
Not sure if this is universally true or depends on the version but def error_404(request): data = {} return render(request,'myapp/error_404.html', data) must be written instead as def error_404(request, exception): data = {} return render(request,'myapp/error_404.html', data) Otherwise I kept receiving an error that it was taking the wrong number of arguments.

Related Articles:
Solving Django Error: TemplateDoesNotExist at /app_name/
How to solve TemplateDoesNotExist error in Django projects, reason for TemplateDoesNotExist error in Django, fixing TemplateDoesNotExist error in Django...
Solving Django error 'NoReverseMatch at' URL with arguments '()' and keyword arguments '{}' not found.
Solving Django error 'NoReverseMatch at' URL with arguments '()' and keyword arguments '{}' not found, URL not found in Django, No reverse match in Django template error...
Displaying custom 404 error (page not found) page in Django 2.0
How to create custom 404 error page in Django, Handler404 example Django 2.0, Page not found error in Django 2.0, Custom Error handlers in Django 2.0...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap