Last Updated : 23 Jul, 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 PageTo 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 Filesviews.py: This code defines three view functions for a Django web application:
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:
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 runserverOutput
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4