Django Custom 404 Page

Adi Ramadhan
2 min readSep 8, 2020

--

Simple customization for 404 not found page. Django 3.0, official docs here.

Default Django 404 page
Custom 404 Page

Step 1: Preparation, Create Django Project, Initial Migration
install virtualenv: pip install virtualenv
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.0
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Migrate: python manage.py migrate

Step 2. Create Django Apps
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

Step 3. Preparing Templates Folder
Add folder templates in myapp

Register myapp/templates in myproject/settings.py

os.path.join(BASE_DIR, 'myapp', 'template')

Step 4. Set DEBUG and ALLOWED_HOSTS

Set DEBUG=False and ALLOWED_HOSTS to ‘127.0.0.1’ in myproject/settings.py

DEBUG = FalseALLOWED_HOSTS = [‘127.0.0.1’]

Step 5. Add 404.html file in myapp/templates folder

myapp/templates/404.html

Step 6. Update myapp/views.py

myapp/views.py

Step 7. Update myproject/urls.py
handler404 will override the default 404 page.

myproject/urls.py

Step 8. Run and Try it in browser

python manage.py runserver

try to access page that not available in app, such as 127.0.0.1/not_found.

--

--

Adi Ramadhan
Adi Ramadhan

No responses yet