0% found this document useful (0 votes)
39 views2 pages

Django Local 404 Page

This document provides instructions for adding a custom 404 page in a Django project. It shows how to create a new Django project, test the default 404 page, configure settings to disable debug mode and restrict allowed hosts, create a templates folder and 404.html file, and add basic text to display a custom 404 message. Following these steps allows testing and customizing the 404 page that will be displayed for pages that do not exist.

Uploaded by

Ariel Cupertino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Django Local 404 Page

This document provides instructions for adding a custom 404 page in a Django project. It shows how to create a new Django project, test the default 404 page, configure settings to disable debug mode and restrict allowed hosts, create a templates folder and 404.html file, and add basic text to display a custom 404 message. Following these steps allows testing and customizing the 404 page that will be displayed for pages that do not exist.

Uploaded by

Ariel Cupertino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Django Local 404 Page


Nov 16, 2018

Adding a custom 404 page is a best practice for any Django website. This post shows a quick
tip for how to test and configure a custom 404 page in your Django projects.

Create a new Django project.

$ cd Desktop
$ mkdir demo && cd demo
$ pipenv install django==2.1
$ pipenv shell
(demo) $ django-admin startproject demo_project .
(demo) $ python manage.py runserver

The Django welcome page is visible at http://127.0.0.1:8000/.

Now go to any other page which should result in a 404. For example http://127.0.0.1:8000/
abcdef.

In the default settings.py file Django sets DEBUG = True and ALLOWED_HOSTS
= []. We want to change both. Turning off debug will show us what a live site would show
and ALLOWED_HOSTS restricts which HTTP requests Django will respond to so the URL
needs to be explicitly added.
# demo_project/settings.py
DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']

Refresh the page again at http://127.0.0.1:8000/abcdef.

Now let’s update it with custom text. First create a templates folder and then add a
404.html file to it.
(demo) $ mkdir templates
(demo) $ touch templates/404.html

Update settings.py so Django will look for this new templates folder.
# demo_project/settings.py
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
And finally add some basic text to the file.
<!-- templates/404.html -->
<h1>404 Page Not Found</h1>
<p>This page does not exist yet!</p>

Now refresh the page once more to see our work.

Links:
https://wsvincent.com/django-local-404-page/

You might also like