Python Django Handling Custom Error Page
Last Updated :
24 Apr, 2025
To handle error reporting in Django, you can utilize Django's built-in form validation mechanisms and Django's error handling capabilities. In this article, I'll demonstrate how to implement error handling. If there are errors in the form submission, the user will be notified of the errors.
Required Modules
Python Django Handling Custom Error Page
To start the project use this command
django-admin startproject queryexpressionsproject
cd my_app
To start the app use this command
python manage.py startapp app
Now add this app to the 'settings.py'

Setting up the Django Files
views.py: This code defines three view functions for a Django web application:
- home(request): Returns a "Hello, World!" response for the root URL.
- error_404(request, exception): Handles HTTP 404 errors (Not Found) and renders a 404 template.
- error_500(request): Handles HTTP 500 errors (Internal Server Error) and renders a 500 template (though there's a potential typo in the template name).
Python3
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.db import models
from .models import Product
def home(request):
return HttpResponse('Hello, World!')
def error_404(request, exception):
print(hh)
return render(request, 'myapp/error_404.html', status=404)
def error_500(request):
return render(request, 'myapp/error_505.html', status=500)
Setting Up GUI
404.html
Create custom error HTML templates in the templates directory of your app. In this case, let's create error_404.html
HTML
<!-- error_404.html -->
<!DOCTYPE html>
<html>
<head>
<title>404 Error</title>
</head>
<body>
<h1>Page Not Found (404)</h1>
<p>The requested page does not exist.</p>
</body>
</html>
505.html
Create custom error HTML templates in the templates directory of your app. In this case, let's create error_505.html
HTML
<!-- error_500.html -->
<!DOCTYPE html>
<html>
<head>
<title>500 Error</title>
</head>
<body>
<h1>Server Error (500)</h1>
<p>Oops! Something went wrong on the server.</p>
</body>
</html>
app/urls.py: This file is used to define the URLs inside the app.
Python3
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.home, name='home'),
]
urls.py: We set handler404 and handler500 to the custom error view functions we defined earlier (error_404 and error_500).
Now, when a 404 or 500 error occurs in your Django project, Django will use the custom error views and render the corresponding custom error pages you've defined.
Python3
from django.contrib import admin
from django.urls import path, include
# Custom 404 error view
handler404 = 'my_app.views.error_404'
# Custom 500 error view
handler500 = 'my_app.views.error_500'
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
]
Things to Remember
If you're seeing the default Django 404 page instead of your custom error_404.html template, it could be due to a few reasons. To ensure your custom 404 error page is displayed, please check the following:
- Template Location: Verify that your error_404.html template is placed in the correct directory structure within your app's templates. The correct path should be products/templates/products/error_404.html. Ensure that the template file name and location are spelled correctly and match the app's name.
- App Name: Confirm that the name of your app is indeed "products" as it appears in the template path and in the handler404 setting.
- Case Sensitivity: Ensure that the file and directory names in your file system are using the correct case sensitivity. On some systems, file and directory names are case-sensitive, so make sure they match exactly.
- Restart Server: After adding or modifying template files, sometimes you may need to restart your development server for changes to take effect. Stop the server (Ctrl+C) and then start it again with python manage.py runserver.
- Debug Mode: The message you provided suggests that you are running your project in Debug mode (DEBUG = True in your Django settings). In Debug mode, Django might still display its default error pages. Custom error pages are typically shown when DEBUG is set to False in your settings.

Deployement of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output
As you can see that if we write correct URLs we can see the hello world page.

Now you can see that we written wrong url and error page occured.

Similar Reads
Python IMDbPY - Error Handling
In this article we will see how we can handle errors related to IMDb module of Python, error like invalid search or data base error network issues that are related to IMDbPY can be caught by checking for the imdb.IMDbErrorexceptionIn order to handle error we have to import the following  from imdb
2 min read
Error Handling in Python using Decorators
Decorators in Python is one of the most useful concepts supported by Python. It takes functions as arguments and also has a nested function. They extend the functionality of the nested function. Example: Python3 # defining decorator function def decorator_example(func): print("Decorator called
2 min read
error_messages - Django Built-in Field Validation
Built-in Field Validations in Django models are the validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field. error
4 min read
Python Exception Handling
Python Exception Handling handles errors that occur during the execution of a program. Exception handling allows to respond to the error, instead of crashing the running program. It enables you to catch and manage errors, making your code more robust and user-friendly. Let's look at an example:Handl
7 min read
Custom Field Validations in Django Forms
This article revolves around how to add custom validation to a particular field. For example to add validation of an email to a CharField by specifying a particular format. There can be multiple ways on how to achieve custom validation. In this article, we are going to show it from the form itself s
2 min read
Python | Extending and customizing django-allauth
Django-allauth is a powerful Django package that simplifies user authentication, registration, account management, and integration with social platforms like Google, Facebook, etc. It builds on Djangoâs built-in authentication system, providing a full suite of ready-to-use views and forms.Prerequisi
4 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
Built-in Error Views in Django
Whenever one tries to visit a link that doesn't exist on a website, it gives a 404 Error, that this page is not found. Similarly, there are more error codes such as 500, 403, etc. Django has some default functions to for handle HTTP errors. Let's explore them one-by-one. Built-in Error Views in Djan
3 min read
Multiple Exception Handling in Python
Given a piece of code that can throw any of several different exceptions, and one needs to account for all of the potential exceptions that could be raised without creating duplicate code or long, meandering code passages. If you can handle different exceptions all using a single block of code, they
3 min read
How to Write Custom Lookups in Django?
Custom lookups allow you to define custom filters that can be used in database queries. By default, Django provides a set of built-in lookups such as exact, icontains, gt, lt, etc., which allow you to perform common filtering operations on query sets. However, sometimes you may need to perform more
3 min read