Welcome back, everyone, in this lecture, we're going to show you how to create your own custom 404
template.
Now, many pages, such as an administration page or a forum for Page, actually come with built in
templates provided by jingo for your convenience, and you have no obligation to have to change these.
You could just leave them as the defaults.
However, you do have the ability to overwrite many of these built in templates in order to give your
website a more uniform look or feel.
Let's go ahead and explore an example of this by overwriting the default 404 template.
Let's head back to our code editor.
OK, here I am back at our my site directory, and what I'm going to do is recall we have this base
that each HTML inside the project level templates.
When you're changing, overwriting things that are typically happening at a project level like a 404
page, which should show up regardless of what application you're looking at.
You're probably going to want to put that template on a project level templates directory.
So inside of templates, which is under my sites, I'm going to create a new file.
And we will call this forum for Timo.
And then here it's going to be just a very simple file.
I'll go ahead and just copy and paste my each HTML head here and inside the body for this HTML.
So let's actually put in the closing tags as well, as well as the closing tag for each HTML.
Here I'm just going to put a heading and let's just have the say my custom 404 page.
That way, we know that we're looking at our particular custom for a four page.
So go ahead and save that so you can technically write whatever you want here.
Just keep in mind, this is underneath templates, which is underneath the root directory.
My site?
Same as based on HTML.
So now what I would like to do is if I try to visit a page that doesn't exist in my web site, I see
my custom for a four page.
Now keep in mind if I were to go to my website right now and it would have a page not found error.
It would show me for A4, but it's showing you a default view because I haven't overwritten anything
yet.
And technically you're seeing this because you have debug equal to true.
And if you change that to false jingo will display a standard 404 page, which is actually what we're
looking for replacement here.
So let's go ahead and change debug equal to false.
That way, I can see the default page, so I'm going to now go to my settings.
So we'll come to my site.
Settings that pie, and I'm going to change a couple of things here, I'm going to change debug.
Equal to false and because I have debug equal to false, I have to tell it.
What are the allowed hosts in order for me to view this?
So I'm just going to say one two seven zero zero one.
Go ahead and save those changes and then we will relaunch our site.
And then we're going to do is refresh this page.
That's four 04.
So now notice on the refresh.
I'm getting my custom for a four page.
So you might think everything is done and Django is smart enough to just look up that four or four,
that HTML.
It saw that you overrode it in the templates directory underneath the project level.
And everything's OK.
Well, that is technically true.
You may not want to call it four zero for that HTML, which is the default h html that Django looks
for.
In fact, let's experiment with changing this name.
So I'm going to take my four or four HTML file.
I'm going to close it and let me try renaming it.
So we're going to say rename this to error underscore view and then go ahead and save.
That changed everything.
Still the same.
I just happened to change the HTML file name.
So now if I were to refresh this, it says not found the requested resource was not found on the server.
So this is telling you, Oh, Django has a default HTML template called for zero four and it will be
able to be overwritten if you just go to a project level template directory and happen to call your
file for or for the HTML.
But what happens if maybe you don't want to deal with a numbering system and you want to call your HTML
file something like error?
View that HTML?
Can we actually have this each HTML file not called for or for HTML overwrite the 404 page?
And yes, you can.
You just have to use a specific status and request.
So what we're going to do is underneath the you URLs that pie that is happening under eight sites page.
We need to do is make sure that this is going to align with the 404 handler, which means I need to
create a view specifically for my 404 page in order for this custom name to overwrite it.
Obviously, it's way easier if you just say for for each HTML, it'll all work out, but let's go ahead
and experiment with something that is not called for or for the HTML.
So underneath my site or create a new file.
And it will be called views that pie.
And inside of used up high, I'm going to set up the view for my custom 404 page.
So we're going to say from jingo dot shortcuts.
Import render.
Make sure we spell import, right?
And then what I'm going to say here is the.
And we can call it my custom page underscore not found view and then put in the request and also taken
an exception.
An exception, because if you read this page, there's been some sort of error.
Now you may be wondering why have such a long custom name for this view?
It can technically be called whatever you want, but if you look up in the documentation, they use
this really long name.
So I'm just matching it to the documentation, but you could, technically speaking, call this whatever
you want.
OK, so we have my custom page not found of you.
Then we're going to return a render, and now we're just going to grab the request and we're going to
say error underscore view the HTML.
And we're also going to happen to report back the status as a 404 error.
Now all I need to do is connect this custom view going to this custom HTML code for four or four two
URLs that pie.
So now I go to my URLs that PI recalls the zeros that pi that is happening at a site or project level
and then at the very bottom outside of URL patterns list, I'm going to add Handler.
Handler, four, zero, four, and I set that equal to the string pathway to wherever my error of is
along with my view, so I can say.
My site.
Views that my custom page not found.
View.
All right.
Now, Handler 404 is a specific variable that Jingle looks for.
You can check out the documentation on the variety of specific handlers that Django has and actually
have it linked for you in our notes.
There's basically a whole page on customizing error based views.
So if you go to that link, it will look something like this.
Let me just bring it out for you.
So you have this ability to customize error views, and you can use Handler for a four four four four
error.
There's a 500 error, 403 error view, if 400.
And notice here they're just saying, OK, go to my views and then show them this custom view, which
is then linked to a custom template.
So this is the way that way you can just do and call whatever you want for your template name instead
of having to have it called for or for the HTML.
In general, I would recommend that you actually go through this anyways and just have it point to an
HTML file called for or for this part of the most robust way to do this.
But let's make sure that this actually works.
So to come back now that everything's been defined and the handler is pointing.
Refresh this and I see my custom for a four page.
So that's kind of the long way to do this.
If you decide not to call it for or for the HTML, instead calling it error view again, our recommendation
is to go ahead and make sure the handler is the fine for anyone viewing this, but also just come here
and renamed us to four for each HTML.
So this is probably the best way of doing it.
Calling your template 404 each HTML and setting up your handler so its handler 404, then the pathway
to find your custom view and then you have a view specifically taking in that exception and registering
that status with a particular HTML file.
In this case, you would call this again for a for the HTML.
All right.
So that's it.
Again, pretty much these directions are linked for you inside our notes, but you get the idea create
a template called for.
Before that, it's HTML.
Create a view for it.
Make sure it also takes an exception and returns to set for 04.
And then the last tricky part is to remember in your URL, stop by to add in this handler outside of
your all patterns and link it to that particular view.
OK, that's it.
I'll see at the next lecture.