Easy Way to Customize Error Handling Page in Django

Erwin Yulizar F
2 min readSep 20, 2021

Hi all, back with me again, Erwin .Today, I will explain how to customize your django error handling page such as Page Not Found(404), Internal Service Error (500), or Forbidden (403).

For your Information, I’m using Django 3.x version in here. Maybe many of you confused and said “Why my 404 page keep showing default one ??”,”Hmm.. I’m pretty sure I already override handler method.”, and etc. Don’t worry here are 4 easy step to fix that.

1. Add your project name in settings.py

Open settings.py then scroll down until you find INSTALLED_APPS, then add your app name below :

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'YOUR_APP_NAME'
]

2. Set your template

Still on settings.py find TEMPLATES and ‘DIRS’ then add path to templates like this bold one :

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR.joinpath('templates'))],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

3. Switch Debug status

Still on the same file settings.py, find DEBUG and change value from TRUE to FALSE :

DEBUG = False

Note: if you want to still use debug, should add -- insecure params when you run your django project e.g : python manage.py runserver — — insecure

4. Add Your Custom Error Handling Page

Last, add your custom error handling page with error_code.html in templates folder :

Error Handling Pages

5. Run your Project

That’s All

Now you can see your custom error handling page. You don’t need to add route in urls.py and views.py anymore. Have a nice day!

--

--

Erwin Yulizar F

I'm a backend developer, love to writing code in python. Love to play games. The best feeling while doing code is when your code is succesfully running :)