Displaying custom 404 error (page not found) page in Django 2.0

showing custom 404 error page in Django 2.0

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 error page and then ask them to click on a link to go to home page.
  • Create your own funny/awesome/informative custom 404 error page.

In this article we will discuss the third option i.e. How to show your own error page in Django 2.0 project when a URL is not found.

Code is available on Github.

Featured Image source:  https://www.pinterest.com/pin/101612535313085400/

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.

default 404 page in django
Default 404 error page in Django

 

Steps to show custom 404 error page in Django:

Project Setup:

– Create a virtual environment using python3 and activate it.

– Once virtual environment is activated, install the Django 2.0 in it using pip.

– Now go to the directory where you want to create project.

– Run the below command to create a new project.

This will create a new folder error_404  in your current working directory. Inside project directory there will be manage.py  file.

– Now we will use manage.py  file to run django commands.

Create a new app inside project. Use the below command to create new app.

This will add a new app in your project. Tree structure of your project at this point will look like as below:

– Don’t forget to add your newly created app to the list of installed apps in project settings.py  file.

error_404/settings.py:

 

– Right now if you start your python server using command python manage.py runserver , you will see below output in your terminal.

And you can see your project is working just fine by going to  localhost:8000 in your browser. If you seen below screen means all is good.

django 2.0
django 2.0 first screen

 

Showing 404 error page:

– If you try to go to some URL which doesn’t exists yet like localhost:8000/abc , then it will show the default 404 page not found error page of Django.

default 404 page not found error page in django
Default 404 page not found error page in Django

– When you will deploy the code on server, you will set DEBUG = False  and ALLOWED_HOST = ["your_domain"]  hence your incorrect URL will return response as shown below.

404 in django
404 error when DEBUG = False

On your localhost you need to edit your project/settings.py  file to show this error.

error_404/settings.py:

Setting “*” in allowed hosts will let you run your project with any host when debug is false. Don’t do this in production.

– Now go inside your app and create urls.py  file.

myapp/urls.py:

 

Custom 404 error page:

– Now create a new view inside your views.py file. Mind the second parameter exception  in error view.

myapp/views.py:

 

– Create an HTML file which will show appropriate message or form or funny stuff in myapp/templates/myapp  directory.

error_404.html:

 

– Now go to urls.py  file of your project and update it.

file error_404/urls.py will look like as below:

 

– Now if you go to your browser and open any non-existent URL, you will be shown custom 404 error page.

Custom 404 error page in Django 2.0
Custom 404 error page in Django 2.0

– We need to set DEBUG = False and ALLOWED_HOST = [“*”] or whatever your host is in order to see custom 404 error page.

Code:

Code is available on Github. Please go through README file to setup the project on your machine.

 

References:
  1. https://docs.djangoproject.com/en/2.0/ref/views/#error-views

 

Featured Image Source:  https://www.pinterest.com/pin/101612535313085400/

 

Comment in case of any query.

(Visited 267 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *