How to Create Custom Error Page in Django?

Published On: 13/12/2022 | Category: Django


Hi Dev,

This article will give you example of how to create custom error page in django. you'll learn how to create 404 error page in django. This article will give you simple example of django 404 page template. I explained simply step by step django custom error page. Here, Creating a basic example of custom error page in django.

All error pages in django have a fairly basic look by default, however occasionally we need a unique error page that matches our design theme. So, in this article, I'll walk you through the process of making your own custom error page for a django application.

Here i explained simply step by step example of how to create custom error page in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core

Step 3: Update setting.py

Then update INSTALLED_APPS within our settings.py file to notify Django about the app.

settings.py
....
INSTALLED_APPS = [

    'core'

]

Let's attempt to access a page that doesn't exist on our website. I might visit a href="https://127.0.0.1:8000/https://127.0.0.1:8000/<random string> as an example.



Because DEBUG = True is specified in your Django settings file, you are receiving this issue. If you set that to False, Django will show a typical 404 page.

Let's accomplish it by disabling Django debug mode. To accomplish this, we must change the

settings.py
....
INSTALLED_APPS = [

    'core'

]

Debug = False
ALLOWED_HOSTS = ['127.0.0.1']

import os

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

You must modify ALLOWED HOSTS in production to 'mydomain.com' Update the page:



Step 4: Creating the Views

In this step, we need to configure views. The error_404_view page will just be a template. open the core/views.py file and add:

core/views.py
from django.shortcuts import render

# Create your views here.

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

Step 5: Creating the Templates

Next, then with your text editor create new templates files: core/templates/404_error_page file and the add:

core/templates/404_error_page
<!DOCTYPE html>
<html lang="en">
<head>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <style type="text/css">
        body{
          margin-top: 150px;
          background-color: #C4CCD9;
        }
        .error-main{
          background-color: #fff;
          box-shadow: 0px 10px 10px -10px #5D6572;
        }
        .error-main h1{
          font-weight: bold;
          color: #444444;
          font-size: 150px;
          text-shadow: 2px 4px 5px #6E6E6E;
        }
        .error-main h6{
          color: #42494F;
          font-size: 20px;
        }
        .error-main p{
          color: #9897A0;
          font-size: 15px; 
        }
    </style>
</head>
<body>
  
    <div class="container">
      <div class="row text-center">
        <div class="col-lg-6 offset-lg-3 col-sm-6 offset-sm-3 col-12 p-3 error-main">
          <div class="row">
            <div class="col-lg-8 col-12 col-sm-10 offset-lg-2 offset-sm-1">
              <h1 class="m-0">404</h1>
              <h6>Page not found - Tuts-Station.com</h6>
              <p>Lorem ipsum dolor sit <span class="text-info">amet</span>, consectetur <span class="text-info">adipisicing</span> elit, sed do eiusmod.</p>
            </div>
          </div>
        </div>
      </div>
    </div>
      
</body>
</html>

Step 6: Creating URLs

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

handler404 = "core.views.error_404_view"

I hope it will help you....

Happy Coding!