1
00:00:05,290 --> 00:00:09,880
Welcome back, everyone, in this lecture, we're going to show you how to create your own custom 404

2
00:00:09,880 --> 00:00:10,330
template.

3
00:00:11,520 --> 00:00:17,130
Now, many pages, such as an administration page or a forum for Page, actually come with built in

4
00:00:17,130 --> 00:00:22,730
templates provided by jingo for your convenience, and you have no obligation to have to change these.

5
00:00:22,740 --> 00:00:24,300
You could just leave them as the defaults.

6
00:00:24,660 --> 00:00:29,490
However, you do have the ability to overwrite many of these built in templates in order to give your

7
00:00:29,490 --> 00:00:32,280
website a more uniform look or feel.

8
00:00:32,729 --> 00:00:37,350
Let's go ahead and explore an example of this by overwriting the default 404 template.

9
00:00:37,710 --> 00:00:39,360
Let's head back to our code editor.

10
00:00:39,900 --> 00:00:47,220
OK, here I am back at our my site directory, and what I'm going to do is recall we have this base

11
00:00:47,220 --> 00:00:50,580
that each HTML inside the project level templates.

12
00:00:50,970 --> 00:00:55,860
When you're changing, overwriting things that are typically happening at a project level like a 404

13
00:00:55,860 --> 00:00:59,310
page, which should show up regardless of what application you're looking at.

14
00:00:59,580 --> 00:01:03,330
You're probably going to want to put that template on a project level templates directory.

15
00:01:03,810 --> 00:01:07,860
So inside of templates, which is under my sites, I'm going to create a new file.

16
00:01:08,720 --> 00:01:11,870
And we will call this forum for Timo.

17
00:01:12,870 --> 00:01:15,690
And then here it's going to be just a very simple file.

18
00:01:15,720 --> 00:01:23,880
I'll go ahead and just copy and paste my each HTML head here and inside the body for this HTML.

19
00:01:23,940 --> 00:01:29,700
So let's actually put in the closing tags as well, as well as the closing tag for each HTML.

20
00:01:30,180 --> 00:01:37,050
Here I'm just going to put a heading and let's just have the say my custom 404 page.

21
00:01:37,110 --> 00:01:41,610
That way, we know that we're looking at our particular custom for a four page.

22
00:01:42,030 --> 00:01:45,000
So go ahead and save that so you can technically write whatever you want here.

23
00:01:45,300 --> 00:01:49,140
Just keep in mind, this is underneath templates, which is underneath the root directory.

24
00:01:49,170 --> 00:01:49,770
My site?

25
00:01:50,010 --> 00:01:51,660
Same as based on HTML.

26
00:01:52,140 --> 00:01:57,480
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

27
00:01:57,480 --> 00:01:59,610
my custom for a four page.

28
00:01:59,760 --> 00:02:06,120
Now keep in mind if I were to go to my website right now and it would have a page not found error.

29
00:02:06,420 --> 00:02:11,970
It would show me for A4, but it's showing you a default view because I haven't overwritten anything

30
00:02:11,970 --> 00:02:12,180
yet.

31
00:02:12,570 --> 00:02:16,860
And technically you're seeing this because you have debug equal to true.

32
00:02:17,250 --> 00:02:21,750
And if you change that to false jingo will display a standard 404 page, which is actually what we're

33
00:02:21,750 --> 00:02:23,460
looking for replacement here.

34
00:02:23,790 --> 00:02:27,160
So let's go ahead and change debug equal to false.

35
00:02:27,240 --> 00:02:32,760
That way, I can see the default page, so I'm going to now go to my settings.

36
00:02:33,300 --> 00:02:35,490
So we'll come to my site.

37
00:02:36,540 --> 00:02:41,310
Settings that pie, and I'm going to change a couple of things here, I'm going to change debug.

38
00:02:42,380 --> 00:02:45,980
Equal to false and because I have debug equal to false, I have to tell it.

39
00:02:46,280 --> 00:02:48,320
What are the allowed hosts in order for me to view this?

40
00:02:48,590 --> 00:02:52,190
So I'm just going to say one two seven zero zero one.

41
00:02:52,730 --> 00:02:56,720
Go ahead and save those changes and then we will relaunch our site.

42
00:02:57,620 --> 00:03:00,240
And then we're going to do is refresh this page.

43
00:03:00,240 --> 00:03:01,040
That's four 04.

44
00:03:01,820 --> 00:03:03,710
So now notice on the refresh.

45
00:03:03,980 --> 00:03:06,420
I'm getting my custom for a four page.

46
00:03:06,440 --> 00:03:10,700
So you might think everything is done and Django is smart enough to just look up that four or four,

47
00:03:10,700 --> 00:03:11,450
that HTML.

48
00:03:11,690 --> 00:03:15,680
It saw that you overrode it in the templates directory underneath the project level.

49
00:03:15,680 --> 00:03:16,810
And everything's OK.

50
00:03:17,180 --> 00:03:18,530
Well, that is technically true.

51
00:03:18,920 --> 00:03:24,860
You may not want to call it four zero for that HTML, which is the default h html that Django looks

52
00:03:24,860 --> 00:03:25,200
for.

53
00:03:25,280 --> 00:03:28,610
In fact, let's experiment with changing this name.

54
00:03:29,060 --> 00:03:32,150
So I'm going to take my four or four HTML file.

55
00:03:32,870 --> 00:03:35,770
I'm going to close it and let me try renaming it.

56
00:03:35,780 --> 00:03:42,740
So we're going to say rename this to error underscore view and then go ahead and save.

57
00:03:42,740 --> 00:03:43,540
That changed everything.

58
00:03:43,560 --> 00:03:44,150
Still the same.

59
00:03:44,150 --> 00:03:46,370
I just happened to change the HTML file name.

60
00:03:46,880 --> 00:03:53,720
So now if I were to refresh this, it says not found the requested resource was not found on the server.

61
00:03:54,410 --> 00:04:02,240
So this is telling you, Oh, Django has a default HTML template called for zero four and it will be

62
00:04:02,240 --> 00:04:07,820
able to be overwritten if you just go to a project level template directory and happen to call your

63
00:04:07,820 --> 00:04:09,800
file for or for the HTML.

64
00:04:10,310 --> 00:04:15,680
But what happens if maybe you don't want to deal with a numbering system and you want to call your HTML

65
00:04:15,680 --> 00:04:16,880
file something like error?

66
00:04:16,880 --> 00:04:17,990
View that HTML?

67
00:04:18,380 --> 00:04:25,610
Can we actually have this each HTML file not called for or for HTML overwrite the 404 page?

68
00:04:25,730 --> 00:04:26,900
And yes, you can.

69
00:04:27,200 --> 00:04:30,680
You just have to use a specific status and request.

70
00:04:31,190 --> 00:04:38,450
So what we're going to do is underneath the you URLs that pie that is happening under eight sites page.

71
00:04:38,780 --> 00:04:44,930
We need to do is make sure that this is going to align with the 404 handler, which means I need to

72
00:04:44,930 --> 00:04:50,120
create a view specifically for my 404 page in order for this custom name to overwrite it.

73
00:04:50,510 --> 00:04:54,740
Obviously, it's way easier if you just say for for each HTML, it'll all work out, but let's go ahead

74
00:04:54,740 --> 00:04:58,490
and experiment with something that is not called for or for the HTML.

75
00:04:59,000 --> 00:05:01,100
So underneath my site or create a new file.

76
00:05:02,080 --> 00:05:03,670
And it will be called views that pie.

77
00:05:04,860 --> 00:05:10,400
And inside of used up high, I'm going to set up the view for my custom 404 page.

78
00:05:10,410 --> 00:05:13,650
So we're going to say from jingo dot shortcuts.

79
00:05:14,250 --> 00:05:15,550
Import render.

80
00:05:16,260 --> 00:05:17,370
Make sure we spell import, right?

81
00:05:19,310 --> 00:05:21,300
And then what I'm going to say here is the.

82
00:05:22,910 --> 00:05:32,630
And we can call it my custom page underscore not found view and then put in the request and also taken

83
00:05:32,630 --> 00:05:33,440
an exception.

84
00:05:35,970 --> 00:05:39,480
An exception, because if you read this page, there's been some sort of error.

85
00:05:40,080 --> 00:05:43,920
Now you may be wondering why have such a long custom name for this view?

86
00:05:44,130 --> 00:05:47,940
It can technically be called whatever you want, but if you look up in the documentation, they use

87
00:05:47,940 --> 00:05:49,470
this really long name.

88
00:05:49,470 --> 00:05:53,550
So I'm just matching it to the documentation, but you could, technically speaking, call this whatever

89
00:05:53,550 --> 00:05:53,970
you want.

90
00:05:54,780 --> 00:05:57,520
OK, so we have my custom page not found of you.

91
00:05:57,540 --> 00:06:04,350
Then we're going to return a render, and now we're just going to grab the request and we're going to

92
00:06:04,350 --> 00:06:07,470
say error underscore view the HTML.

93
00:06:08,070 --> 00:06:12,750
And we're also going to happen to report back the status as a 404 error.

94
00:06:13,530 --> 00:06:21,480
Now all I need to do is connect this custom view going to this custom HTML code for four or four two

95
00:06:21,510 --> 00:06:22,470
URLs that pie.

96
00:06:22,980 --> 00:06:28,940
So now I go to my URLs that PI recalls the zeros that pi that is happening at a site or project level

97
00:06:29,400 --> 00:06:35,640
and then at the very bottom outside of URL patterns list, I'm going to add Handler.

98
00:06:36,660 --> 00:06:45,410
Handler, four, zero, four, and I set that equal to the string pathway to wherever my error of is

99
00:06:45,420 --> 00:06:46,860
along with my view, so I can say.

100
00:06:47,900 --> 00:06:48,680
My site.

101
00:06:49,690 --> 00:06:56,050
Views that my custom page not found.

102
00:06:56,200 --> 00:06:56,680
View.

103
00:06:57,860 --> 00:06:58,250
All right.

104
00:06:58,280 --> 00:07:03,920
Now, Handler 404 is a specific variable that Jingle looks for.

105
00:07:03,990 --> 00:07:10,070
You can check out the documentation on the variety of specific handlers that Django has and actually

106
00:07:10,070 --> 00:07:12,900
have it linked for you in our notes.

107
00:07:12,920 --> 00:07:16,790
There's basically a whole page on customizing error based views.

108
00:07:17,300 --> 00:07:19,670
So if you go to that link, it will look something like this.

109
00:07:19,700 --> 00:07:21,080
Let me just bring it out for you.

110
00:07:22,040 --> 00:07:27,950
So you have this ability to customize error views, and you can use Handler for a four four four four

111
00:07:27,950 --> 00:07:28,310
error.

112
00:07:28,610 --> 00:07:31,760
There's a 500 error, 403 error view, if 400.

113
00:07:32,060 --> 00:07:36,740
And notice here they're just saying, OK, go to my views and then show them this custom view, which

114
00:07:36,740 --> 00:07:38,900
is then linked to a custom template.

115
00:07:39,230 --> 00:07:44,840
So this is the way that way you can just do and call whatever you want for your template name instead

116
00:07:44,840 --> 00:07:47,390
of having to have it called for or for the HTML.

117
00:07:47,930 --> 00:07:52,700
In general, I would recommend that you actually go through this anyways and just have it point to an

118
00:07:52,820 --> 00:07:55,730
HTML file called for or for this part of the most robust way to do this.

119
00:07:56,030 --> 00:07:57,800
But let's make sure that this actually works.

120
00:07:58,250 --> 00:08:01,550
So to come back now that everything's been defined and the handler is pointing.

121
00:08:02,000 --> 00:08:04,850
Refresh this and I see my custom for a four page.

122
00:08:05,780 --> 00:08:07,820
So that's kind of the long way to do this.

123
00:08:08,150 --> 00:08:14,240
If you decide not to call it for or for the HTML, instead calling it error view again, our recommendation

124
00:08:14,240 --> 00:08:20,270
is to go ahead and make sure the handler is the fine for anyone viewing this, but also just come here

125
00:08:20,270 --> 00:08:24,020
and renamed us to four for each HTML.

126
00:08:24,200 --> 00:08:25,790
So this is probably the best way of doing it.

127
00:08:26,060 --> 00:08:33,559
Calling your template 404 each HTML and setting up your handler so its handler 404, then the pathway

128
00:08:33,559 --> 00:08:39,500
to find your custom view and then you have a view specifically taking in that exception and registering

129
00:08:39,500 --> 00:08:42,169
that status with a particular HTML file.

130
00:08:42,169 --> 00:08:45,710
In this case, you would call this again for a for the HTML.

131
00:08:46,160 --> 00:08:46,460
All right.

132
00:08:46,850 --> 00:08:47,690
So that's it.

133
00:08:47,690 --> 00:08:52,670
Again, pretty much these directions are linked for you inside our notes, but you get the idea create

134
00:08:52,670 --> 00:08:53,450
a template called for.

135
00:08:53,450 --> 00:08:54,410
Before that, it's HTML.

136
00:08:54,800 --> 00:08:56,090
Create a view for it.

137
00:08:56,300 --> 00:08:59,420
Make sure it also takes an exception and returns to set for 04.

138
00:08:59,690 --> 00:09:05,090
And then the last tricky part is to remember in your URL, stop by to add in this handler outside of

139
00:09:05,090 --> 00:09:08,240
your all patterns and link it to that particular view.

140
00:09:08,840 --> 00:09:09,390
OK, that's it.

141
00:09:09,410 --> 00:09:10,370
I'll see at the next lecture.

