How to customize error pages in Python Django?

Python Django Handling Custom Error Page

Every web site needs to handle errors like HTTP 404 “page not found” or 500 “internal server errors”. For Django based web server when these errors occur, the web server will show active server’s settings.

I added basic steps about handling Custom Error Page. For production service it should be more critical then development platform disables DEBUG mode and also customize error pages.

By default, Python Django manages these error codes with handlerxxx under urls.py file.

Step 1: Edit settings.py file

Filename: settings.py

— Change debug level

— Define template directory for HTML

— Add project to installed apps

Step 2: Add handler method to urls.py

There are 4 already defined handler methods in django.urls functions. To change default configuration;

For my project “myappname” is “certman”.

Filename: urls.py

Step 3: Edit views.py file

You need to define an error function to the views.py. Add these lines to manage handlerxxx.

Filename: views.py

 

Step 4: Define HTML file (404.html, 500.html, etc)

Add customized HTML files under the templates directory.

Step 5: Restart python Django project

Follow me

Abdurrahim

I'm a System Engineer with extensive experience and administration skills and works for Interbank Card Center Of Turkey.I provide hardware and software support for the following Unix/Linux and Windows platforms.(Oracle Solaris,HP-UX, Linux, IBM-AIX, Windows Servers)
Follow me
facebooktwittergoogle_pluslinkedinby feather

0