1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
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 urls.py  file of your project, import handler404  and handler505 .
  • In urls.py  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'^admin/', admin.site.urls),
        url(r'^myapp/', include('myapp.urls', namespace='myapp')),
    handler404 = myapp_views.error_404
    handler500 = myapp_views.error_500

  • In settings.py 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.

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

1 thought on 'Designing Custom 404 And 500 Error Pages In Django'
David Ogutu :
You should just use the default error page templates e.g. 404.html on your root template dir:https://docs.djangoproject.com/en/2.1/ref/views/#the-404-page-not-found-view
Robertgad :
for short, is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image. .. or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap