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 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course