We all know what errors are, here I am specifically considering HTTP errors that we all have encountered while visiting websites. 404 pages not found, 500 server errors, 403 HTTP forbidden, etc. are some of the HTTP errors. When we design a website we also have to keep in mind that if somehow a user makes an unexpected request or visits an unexpected link, we have to inform him. We have some built-in error views in Django for handling HTTP errors and we can also customize them accordingly. In this blog, we will learn about Built-In views in Django.
404 Error (Page Not Found) view
404 page is rendered whenever a user tries to visit a non-existing page. By default it is the view i.e. Django.views.defaults.page_not_found() which loads and renders the 404 templates or gives a NOT FOUND message. This view will take two parameters and pass them to the template, first request_path, the URL that resulted in the error, and second, the exception that triggered the view.
- If there is no valid URL match in URLconf then it is also a 404 error.
- It is never used when DEBUG is set to True.
Note:-The project implementation is the same, for adding the 404 error view just add the following code in views.py, and here 404.html is not built-in you have to create an HTML page for this.
def error_404(request, exception): return render(request,'404.html')
500 (Server Error) View
Whenever there is a runtime error in view code, Django executes special-case behavior or in simple words, a server receives a condition that restricts it from fulfilling the request. If there is an exception caused by views then by default it is the view i.e. Django.views.defaults.server_error() which loads and renders the 500 templates or gives a SERVER ERROR message. This view does not take any parameter for exception and if DEBUG is set to True then this will not be used.
Note:- The project implementation is the same, for adding the 500 error view just add the following code in views.py, and here 500.html is not built-in you have to create an HTML page for this.
def error_500(request, exception): return render(request,'500.html')
403 (Forbidden) View
This error simply states that the server understands your request but does not want to authorize it. If there is an exception caused by views then by default it is the view i.e. Django.views.defaults.permission_denied() which loads and renders the 403 templates or gives a 403 FORBIDDEN message. This view takes exceptions as parameters that are triggered by permission denied exceptions.
This functionality can be added like the code given below, in this code we will raise a 403 forbidden error if other than the teacher wants to change the subjects, where change is the function in the views.py file and we first check if the user is a teacher or not if it is not a teacher then raise the exception otherwise do the changes:-
from django.core.exceptions import PermissionDenied def change(request,id): if not request.user.is_teacher: raise PermissionDenied else: #add your code
Note :- The project implementation is the same, for adding the 403 error view just add the following code in views.py and here 403.html is not built in you have to create an html page for this.
def error_403(request, exception): return render(request,'403.html')
400 (Bad Request) View
There are some of the reason for occurrence of this error :-
- Incorrect URL
- Corrupted browser cache and cookies
- DNS lookup cache
- File size is too large
This is very similar to server error, by default the cause why the exception is triggered is not passed to template context, because the exception may contain sensitive information which can be misused if it falls in wrong hand.
Note :- The project implementation is the same, for adding the 400 error view just add the following code in views.py and here 400.html is not built in you have to create an html page for this.
def error_400(request, exception): return render(request,'400.html')
So, we learned about Built-In error views in Django. I hope this blog will be useful for you and helps you to understand each and every step. Hope all your doubts get cleared. Thank you.