Django: Getting Started Transcripts
Chapter: Users and Account Management
Lecture: Branded error screens
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
You almost always run the deV server in debug mode. You can change what mode you're in by editing the debug property in setting.py
0:08
When debug is true, you get an error page with useful troubleshooting information if something goes wrong.
0:16
You saw one of those in the lesson on templates when I clicked the home URL before the page had been created.
0:23
In production, you don't want these pages, you want something else. You don't want to expose your debug info to the black hats and you also want
0:31
a prettier experience for your users. You can create your own 404 pages but to test them you'll need to make some changes in settings.py.
0:41
The kind of changes I'm going to demonstrate can be done for 403, that's access denied, 404 page not found, and 500 help
0:51
something has gone horribly awry on the server. Let me demonstrate how to do this by creating a custom 404 page.
0:58
First though the changes you'll need in setting.py. To see the custom 404 page
1:06
you're going to need to change a couple of things to make your configuration closer to what you do in production.
1:12
First off, the debug property needs to be set to false. Second is changing the allowed hosts property.
1:22
Django has a security feature that ensures certain http headers are set to the server hosting your site. For this to work properly,
1:30
the allowed hosts configuration has to contain the domain name or IP address of the hosting servers. As I'm going to be running this locally,
1:40
I'll add the local host info to the property. With the configuration out of the way Now, all I have to do is the actual custom page.
1:52
The custom 404 page is named 404.html and goes in the root of the templates directory. Here I've written an example, nothing special about this page.
2:05
It just extends from the base like home or about and overrides the title and content blocks.
2:11
I've still got the dev server running so let's go to the browser. This looks wrong, doesn't it?
2:21
There's a reason for that because debug is set to false the server is no longer serving static files.
2:28
Static files include things like the style sheet and the logo. So this is as good as you're going to get out of the deV server.
2:35
Let's go and do those settings, and now back to that bad URL refreshing and that's the debug page once again.