Built-in Error Views in Django
Last Updated :
01 Nov, 2020
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 Django -
404 view (page not found) -
Normally this view is used by to render basic 404 template when requested URL is not available on the server. This situation happens by default Django raises django.views.defaults.page_not_found() view and render default404 error template. You can customize it by adding custom 404.html page inside templates folder.

500 view (server error) -
This view is used when the server has been crashed or didn't respond to anything. In this case Django has default function django.views.defaults.server_error() to render server error for this view. You can also customize it by adding your 500.html page inside the templates folder.

403 view (Forbidden) -
When user does not have permission to view particular page but he/she requests to view that page. In that case 403 view comes into play. It says page is forbidden and will not show page to that user. For that Django has django.views.defaults.permission_denied() function to render forbidden template. It maintains your privacy and only permitted users can access certain pages. You can also customize it by adding your 403.html page inside the templates folder.
Suppose you have an ecommerce website and you only want authenticated merchants to list products. Buyers and normal users can't list their products. You can add this functionality in Django like this:
from django.core.exceptions import PermissionDenied
def upload(request, pk):
if not request.user.is_merchant:
raise PermissionDenied
# your code here.....

400 view (Bad Request):
The 400 view or Bad Request view is used by Django when someone try to access confidential page of your site which may lead your site to be hacked or leak confidential data. So, you don't want anyone to access that page. Django has django.views.defaults.bad_request() to raise 400 view for any kind of SuspiciousOperation. This prevents your site from Bad peoples.

Customize Built-in Error Views in Django -
Now, let's see how to customize these error views. First of all you need to go into settings.py and set Debug=False.
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Make folder inside the project and name it anything, here I am giving name 'templates' to that folder. Now go to settings.py and set templates directory.
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
Now, inside the templates directory you can create html files '404.html', '500.html', '403.html', '400.html', etc. After creating these pages show your HTML skills and customize pages by yourself. Add your app name into settings.py.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'jquery',
'geeks'
]
Add the handler method to urls.py
handler404 = 'myappname.views.error_404'
handler500 = 'myappname.views.error_500'
handler403 = 'myappname.views.error_403'
handler400 = 'myappname.views.error_400'
Now set logic to show these pages in views.py
from django.shortcuts import render
def error_404(request, exception):
return render(request,'404.html')
def error_500(request, exception):
return render(request,'500.html', data)
def error_403(request, exception):
return render(request,'403.html')
def error_400(request, exception):
return render(request,'400.html', data)
Now, you are all set to run the server and see these error handling pages made by you.
To run server: python manage.py runserver
Similar Reads
Logging Server Errors in Django
When building web applications, logging is an essential tool for developers to monitor the applicationâs health, diagnose issues, and understand user behavior. Django provides robust logging capabilities allowing us to capture, track, and handle server errors effectively. This article will walk you
6 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
Views In Django | Python
Django Views are one of the vital participants of the MVT Structure of Django. As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, a redirect, a 404 error, an XML document, an ima
6 min read
verbose_name - 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. verbo
3 min read
Django Function Based Views
Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. Django is based on MVT (Model View Template) architecture and revolves around CRUD (Create, Retrieve, Up
7 min read
Built-in Field Validations - Django Models
Field validations in Django help make sure the data you enter into your database is correct and follows certain rules. Django automatically checks the data based on the type of field you use, so you donât have to write extra code to validate it yourself.Django provides built-in validations for every
3 min read
Detail View - Function based Views Django
Detail View refers to a view (logic) to display a particular instance of a table from the database with all the necessary details. It is used to display multiple types of data on a single page or view, for example, profile of a user. Django provides extra-ordinary support for Detail Views but let's
3 min read
Delete View - Function based Views Django
Delete View refers to a view (logic) to delete a particular instance of a table from the database. It is used to delete entries in the database for example, deleting an article at geeksforgeeks. So Delete view must show a confirmation message to the user and should delete the instance automatically.
3 min read
Function based Views - Django Rest Framework
Django REST Framework allows us to work with regular Django views. It facilitates processing the HTTP requests and providing appropriate HTTP responses. In this section, you will understand how to implement Django views for the Restful Web service. We also make use of the @api_view decorator. Before
13 min read
Filter data in Django Rest Framework
Django REST Framework's generic list view, by default, returns the entire query sets for a model manager. For real-world applications, it is necessary to filter the queryset to retrieve the relevant results based on the need. Â So, let's discuss how to create a RESTful Web Service that provides filte
4 min read