How Do I Customize Django 404 Error Page?

Most of the times many people will frequently come across the same error page that is 404 error page . Actually the 404 error is a standard HTTP error message code that is raised when there is a client-side errors like passing incorrect URL or poor internet connection and server-side mistakes like the URL called upon doesn’t exist or has not been defined or if the server is down and other things that will lead to 404 error page is because of broken links or the page has been moved to different address that indirectly convey that the server cannot find the required URL customization of 404 error page .

Most of the 404 error page are not given high priority but that 404 error page are the effective tools customized in such a manner that the user should get impress and this will be a effective tool for conveying your brand identity, entertaining visitors and keeping them on your site rather than clicking away in annoyance. Of course, you don’t actually want visitors to find themselves there at all but adding some personality and clever design can add a silver lining to an error.

The below content briefly explains about the customization of 404 error page.

customization of Django 404 error page

404 Error page
should be the most famous status code. In production, a good Django project has to handle this error with your own
custom 404 error page. I will show you how to accomplish this with simple steps.

Let’s create a Django project without losing time.

Open your console and write these lines:

$ cd Desktop
$ mkdir 404_error_page .
$ cd 404_error_page .

Then create a Django project:

                  django-admin startproject error_page_404 .

Using below line we run server

                 python manage.py runserver

If you visit http://127.0.0.1:8000/ you will see the Django welcome page

Django Welcome Page

Let’s try to go to a non-existing page on our website. For example, I will go to
<a href="https://127.0.0.1:8000/https://127.0.0.1:8000/<random_value>.

Default Not Found Page

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Let’s do that by turning off Django debug mode. For this, we need to update the

   settings.py      file


   Debug = False
   ALLOWED_HOSTS = ['127.0.0.1']

Note: In production, you need to change ALLOWED_HOSTS to ‘mydomain.com’

Refresh the page :

Use this command and create a file under the mysite folder

               touch error_page_404/views.py

I know it is weird to have a views.py file under the project directory. We are always
told that views.py is a file that is under each app directory, not the project itself. But
this is the suggested way in the official document.


Your Django project should look like this:

.
├── db.sqlite3
├── manage.py
└── error_page_404
  ├── init .py
  ├── pycache
  ├── asgi.py
  ├── settings.py
  ├── urls.py
  ├── views.py
  └── wsgi.p

Now we will add a function that handles 404 error by putting these lines to the
mysite/views.py file:

from django.shortcuts import render

def error_404_view(request, exception):
return render(request, '404_error_page.html', status=404)

Don’t forget to add this function to the bottom of mysite/urls.py file

In the end, mysite/urls.py file becomes

from django.contrib import admin
from django.urls import path

urlpatterns = [
                 path('admin/', admin.site.urls),
              ]
handler404 = "error_page_404.views.error_404_view"

First, we need to create a templates folder and then we will add a 404.html file to this
folder

$ mkdir templates
$ touch templates/404_error_page.html

You need to tell Django where your templates folder is located so update the
settings.py file.

import os

TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]

In 404_error_page.html you are now able to customize the error message page as you desire. I am
keeping it simple:

<h1>404 Page Not Found</h1>
<p>My Custom 404 Page Not Found!</p>

Before refreshing the page, be sure that your project structure should be like this:

├── manage.py
├── error_page_404
│   ├── init .py
│   ├── pycache
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── templates
    └── 404_error_page.html

Now you can go to any page that does not exist on your website. The 404.html
template will handle all of them.

Customized 404 Not Found Page

Voila! Our custom 404 Page Not Found is visible for any non-existing page on your
website

Thank You

Github link — https://github.com/saikumar248/404_error_page-customization.git