When something goes wrong on your site such as a missing page, a server error, or a permission problem, you will want to display error pages that keep your site's styling and are on brand for your webapp. You can easily create custom templates for common HTTP error codes in Django.
By default, Django automatically looks in your root level templates directory for the following files:
- templates/404.html
- templates/500.html
- templates/403.html
- templates/400.html
These don't require views or URLs and Django will serve them automatically when those errors occur. Therefore, you should create your HTML files here and name them using the names given above.
By default, Django does not display custom error templates while
DEBUG=True.
To test them locally, set
DEBUG=False
and configure
ALLOWED_HOSTS,
or test them once deployed.
Note that due to the location of the error pages, there are no associated views.py files so if you need to load any information that you would normally do through a view, you will need to pass it through a context processor outlined here in the core app section.