1
00:00:05,320 --> 00:00:09,790
Welcome back, everyone, to this lecture where we're going to be discussing responses and for, of

2
00:00:09,790 --> 00:00:10,210
course.

3
00:00:11,280 --> 00:00:16,350
So inevitably, there's going to be a time when a client requests a Web page view that doesn't actually

4
00:00:16,350 --> 00:00:22,380
exist, or they accidentally provided the incorrect URL route that didn't match up with what you were

5
00:00:22,380 --> 00:00:24,060
expecting on your end of things.

6
00:00:24,420 --> 00:00:30,960
In these cases, you can try to preempt an error page by using a dispute response not found to still

7
00:00:30,960 --> 00:00:35,670
return some web page for the user and at least inform them that you weren't able to find what they were

8
00:00:35,670 --> 00:00:36,270
looking for.

9
00:00:37,540 --> 00:00:43,690
Django also has a default for a four page that we can display in case we get a 404 error.

10
00:00:44,140 --> 00:00:49,030
So later on, we're going to have a lot deeper into templates and see how to create our own custom 404

11
00:00:49,030 --> 00:00:49,390
page.

12
00:00:49,630 --> 00:00:54,550
But for right now, I'll just focus on two main functions, which is HTP response not found.

13
00:00:54,670 --> 00:00:57,310
And then the HDP 404 response.

14
00:00:57,610 --> 00:00:59,410
Let's head back to our code editor.

15
00:00:59,890 --> 00:01:00,130
All right.

16
00:01:00,160 --> 00:01:02,530
Here I am back at the website we've been working with.

17
00:01:02,890 --> 00:01:09,670
Recall that we had this dictionary of articles and then a news view that ended up showing you the particular

18
00:01:09,670 --> 00:01:12,280
page for whatever topic was asked for.

19
00:01:12,310 --> 00:01:14,410
And then we had this kind of silly ad view here.

20
00:01:14,710 --> 00:01:17,980
I'm going to collapse this one since we won't be working with it right now.

21
00:01:18,460 --> 00:01:23,710
And then here in URLs that pie, we ended up using that topic and displayed that particular news view

22
00:01:24,010 --> 00:01:25,870
from the articles dictionary.

23
00:01:26,170 --> 00:01:32,890
But right now, if we go to our website and say something like first app for sports, we get the sports

24
00:01:32,890 --> 00:01:33,310
page.

25
00:01:33,580 --> 00:01:38,410
But then if I ask for something that's not there like arts, then I get a key error.

26
00:01:38,800 --> 00:01:43,720
Well, it'd be nice if I could try to preempt this sort of error and just tell the user, Hey, I can't

27
00:01:43,720 --> 00:01:46,090
find anything for this particular topic.

28
00:01:46,270 --> 00:01:47,200
How do we actually do that?

29
00:01:47,650 --> 00:01:52,240
Well, we'll come back to our view and we're going to use a try except statement.

30
00:01:52,780 --> 00:01:56,320
So what we're going to do is we are going to inside of news view.

31
00:01:56,770 --> 00:02:01,360
We're first going to attempt to try make sure our indentations correct here.

32
00:02:01,780 --> 00:02:06,850
We're going to try to figure out that particular headline or page.

33
00:02:07,300 --> 00:02:09,789
So I can say result is equal to.

34
00:02:10,180 --> 00:02:13,420
And then I'm going to say here articles.

35
00:02:14,310 --> 00:02:15,180
Topic past.

36
00:02:15,420 --> 00:02:22,180
So I'm going to try to get that result and then we're going to return, and here we're going to grab

37
00:02:22,710 --> 00:02:26,490
your response article's topic, so we're going to try to do what we just did before.

38
00:02:26,760 --> 00:02:31,560
Now, in the case that we can't find something like arts, that's where we have to have some sort of

39
00:02:31,560 --> 00:02:35,370
exception where we can use the h to the poor response not found.

40
00:02:35,880 --> 00:02:40,830
So what I'm going to do then, is if that fails, I can say, except.

41
00:02:41,280 --> 00:02:47,130
And if the accept line or block is already running, then I know we don't have that topic.

42
00:02:47,640 --> 00:02:50,640
So I could just say result is equal to.

43
00:02:51,870 --> 00:02:53,280
Let's say no page.

44
00:02:54,250 --> 00:02:55,480
For that topic.

45
00:02:57,090 --> 00:03:04,380
And then inside of this block, I'm going to have and then sit here, return, and here I can say HTP

46
00:03:04,380 --> 00:03:05,020
response.

47
00:03:05,700 --> 00:03:07,680
And I say it should be a response.

48
00:03:08,710 --> 00:03:12,100
Not found, and in order to use this, you have to make sure to import it.

49
00:03:12,370 --> 00:03:16,690
So at the top here, I've already imported from response HTP response not found.

50
00:03:17,110 --> 00:03:20,920
This is essentially a further indication that, hey, what you're looking for, I don't actually have

51
00:03:20,920 --> 00:03:21,790
a web page for that.

52
00:03:22,120 --> 00:03:27,190
And then we can still pass in results here, and we're going to save that.

53
00:03:28,300 --> 00:03:32,020
And so now what happens is we come back here and I refresh this.

54
00:03:32,990 --> 00:03:39,770
I say, hey, no page for that topic, but what's nice about this is instead of just doing a normal

55
00:03:39,830 --> 00:03:45,650
HTP response, I'm actually taken to account of the fact that this is technically if we look down at

56
00:03:45,650 --> 00:03:51,290
our terminal a 404 error, meaning there was no page for that particular URL route.

57
00:03:51,830 --> 00:03:57,230
Now I should point out, if you check out the documentation, there are two main pages I want you to

58
00:03:57,230 --> 00:03:57,890
be aware of.

59
00:03:58,280 --> 00:04:04,190
One is the documentation on writing views, so it's just the general topic on writing views.

60
00:04:04,550 --> 00:04:09,350
It talks about a simple view stuff that we've already talked about, but it also has a section on returning

61
00:04:09,350 --> 00:04:09,900
errors.

62
00:04:09,920 --> 00:04:14,450
So the main one that you start off with is HTP response not found.

63
00:04:14,870 --> 00:04:20,029
However, as you continue developing your website would be nice is to switch this out for some sort

64
00:04:20,029 --> 00:04:22,100
of generic 404 exception.

65
00:04:22,520 --> 00:04:28,550
Otherwise, you'd have to keep automatically defining somehow the specific errors you want for every

66
00:04:28,550 --> 00:04:29,210
single view.

67
00:04:29,570 --> 00:04:35,690
Instead, it would be nice if you could just write HTP four or four and then say something like, Hey,

68
00:04:35,690 --> 00:04:40,250
this page doesn't exist or connect it to some 404 template you have later on.

69
00:04:40,670 --> 00:04:46,790
I should also note that there are many subclasses that are not just HTP response and HTP response not

70
00:04:46,790 --> 00:04:47,180
found.

71
00:04:47,510 --> 00:04:52,310
You can check that out on a link here on this page, where it talks about the URL dispatcher, as well

72
00:04:52,310 --> 00:04:54,620
as the requests and views.

73
00:04:54,950 --> 00:05:00,170
There's a request and response objects page that basically has a list of all the different types of

74
00:05:00,170 --> 00:05:03,680
subclasses inside of HTP response and how it works.

75
00:05:03,980 --> 00:05:06,670
So a lot of what we work with here is on this page.

76
00:05:06,680 --> 00:05:12,380
It's a really long page talks about kind of all the different subclasses of HIV response, all different

77
00:05:12,380 --> 00:05:13,160
methods on that.

78
00:05:13,460 --> 00:05:15,590
So you can see here a severe response subclasses.

79
00:05:15,890 --> 00:05:19,790
There's redirects not modified, but requests a step response gone.

80
00:05:20,120 --> 00:05:27,470
So essentially, all those 404 HTML codes in general, four or three for over 400, 500, etc. the main

81
00:05:27,470 --> 00:05:29,630
ones all have subclasses associated with them.

82
00:05:29,990 --> 00:05:35,420
However, really, what we're looking for is an HTP response not found to be replaced by just raising

83
00:05:35,420 --> 00:05:39,860
some sort of generic for a for code that later on, when we learned about templates, we can actually

84
00:05:39,860 --> 00:05:42,380
connect to a specific 404 page.

85
00:05:42,650 --> 00:05:43,820
So how do we do this?

86
00:05:43,850 --> 00:05:47,720
We're going to come back to our code and we're going to do two things here.

87
00:05:48,500 --> 00:05:51,950
Instead of saying accept return response, not found.

88
00:05:52,640 --> 00:06:03,200
I'm going to raise the HP 404 error and say four or four generic error.

89
00:06:03,530 --> 00:06:07,850
And then later on, when we actually learn about templates, we're going to end up doing is this will

90
00:06:07,850 --> 00:06:14,030
actually be connected to something like our custom 404 HTML template that we can connect to later on.

91
00:06:14,330 --> 00:06:19,730
Right now, though, we can actually just raise it as a simple string in order to use each TDP for a

92
00:06:19,730 --> 00:06:21,710
form when you come back up here and import it.

93
00:06:22,280 --> 00:06:25,290
So we have HTP for four.

94
00:06:27,060 --> 00:06:29,040
And let's make sure that's correct there.

95
00:06:30,650 --> 00:06:34,760
All right, so that should be looking good, that's matching up here with the import.

96
00:06:35,060 --> 00:06:40,970
One thing to note, though, is you actually need to adjust your settings, otherwise you'll still get

97
00:06:40,970 --> 00:06:43,190
some sort of debug page.

98
00:06:43,190 --> 00:06:44,780
So even if we save these changes.

99
00:06:45,110 --> 00:06:48,200
Let me show you what happens before we edit the actual settings file.

100
00:06:48,680 --> 00:06:59,390
So I come back to my website here, and if I refresh, I see Page not found for 04 and it says 404 and

101
00:06:59,390 --> 00:07:00,830
then the term actually put here.

102
00:07:00,860 --> 00:07:01,760
Generic error.

103
00:07:02,150 --> 00:07:05,990
If you scroll down here, you'll notice that it says, Hey, you're seeing this particular error page

104
00:07:06,320 --> 00:07:09,860
because you have debug equal to true in your jingo settings file.

105
00:07:10,220 --> 00:07:13,610
Change that to false and jingo will display a standard for a four page.

106
00:07:13,940 --> 00:07:16,040
So let's experiment a little bit with this.

107
00:07:16,580 --> 00:07:20,150
Later on, we'll talk a lot more about project settings, but right now I do want to talk a little bit

108
00:07:20,150 --> 00:07:20,630
about this.

109
00:07:20,900 --> 00:07:22,340
And there's two things here at the change here.

110
00:07:22,580 --> 00:07:27,470
One is our debug settings and the other is our actual allowed hosts list.

111
00:07:27,880 --> 00:07:28,790
Somebody keep in mind, though.

112
00:07:28,820 --> 00:07:33,830
Whenever you're developing a Django project, you do typically want to have debug equal to true that

113
00:07:33,830 --> 00:07:37,640
we can help debug what's happening with the moral force and to see if it's working.

114
00:07:37,640 --> 00:07:42,620
As you expected, when you deploy a website, you definitely want to have debug equals to false.

115
00:07:42,860 --> 00:07:47,180
Otherwise, your users will get some sort of page like this, which gives way too much information about

116
00:07:47,180 --> 00:07:48,920
the website operations to the public.

117
00:07:49,310 --> 00:07:52,550
So we're going to come back to our Visual Studio code.

118
00:07:53,000 --> 00:07:54,290
Go to settings that pie.

119
00:07:55,770 --> 00:08:01,020
And let's actually kill the websites we're going to control, seeing my terminal to not have the website

120
00:08:01,020 --> 00:08:01,650
run anymore.

121
00:08:02,100 --> 00:08:07,860
And we're going to say debug is equal to false and again, security warning don't run debug turn on

122
00:08:07,860 --> 00:08:13,950
in production because there's a debug editing tool and it gives way too much power to any person visiting

123
00:08:13,950 --> 00:08:14,460
your website.

124
00:08:14,880 --> 00:08:19,110
The other thing is, when you have debug equal to false, you need to actually tell Django which hosts

125
00:08:19,110 --> 00:08:19,650
are allowed.

126
00:08:21,020 --> 00:08:26,090
Now, because I'm running this locally, I'm simply going to say the following host is allowed.

127
00:08:26,420 --> 00:08:31,730
It's the one two seven zero zero one that we've been running off of.

128
00:08:31,760 --> 00:08:35,480
So remember, we're running off of this right here, so I'm going to say that's an allowed host.

129
00:08:36,260 --> 00:08:37,380
Save that change.

130
00:08:37,400 --> 00:08:39,380
Let's run our site again.

131
00:08:40,220 --> 00:08:42,080
OK, so the site is now running.

132
00:08:42,289 --> 00:08:47,780
I'm going to come back here and let's refresh the arts page and we see we get not found.

133
00:08:47,900 --> 00:08:50,030
The requested resource was not found on the server.

134
00:08:50,330 --> 00:08:54,730
So that is essentially Django's 404 page that a user would see.

135
00:08:54,740 --> 00:08:58,460
How do we deploy this in real life with debugging turned off?

136
00:08:58,880 --> 00:08:59,210
OK.

137
00:08:59,720 --> 00:09:03,470
So what I'm going to do is turn back here to debug.

138
00:09:04,460 --> 00:09:09,630
Set that equal to true again for the rest of these lectures, and we can delete this allowed hosts.

139
00:09:09,650 --> 00:09:10,700
That's not necessary anymore.

140
00:09:11,210 --> 00:09:12,380
Go ahead and save that.

141
00:09:12,380 --> 00:09:16,430
We'll talk a lot more about settings in general later on, but let's do a quick review of everything

142
00:09:16,430 --> 00:09:23,210
we covered in this particular lecture so you can always use a try except statement in order to take

143
00:09:23,210 --> 00:09:28,400
into account the fact that a client may visit something that doesn't actually exist in the beginning.

144
00:09:28,400 --> 00:09:33,380
You may just want to use it to response not found, especially if you're debugging things later on.

145
00:09:33,380 --> 00:09:36,740
Which you're going to want to do is use some sort of generic HTP for.

146
00:09:37,490 --> 00:09:40,820
And later on, we'll link it to our own custom forum for each HTML template.

147
00:09:41,150 --> 00:09:41,480
All right.

148
00:09:41,630 --> 00:09:42,470
That's it for this lecture.

149
00:09:42,650 --> 00:09:43,460
I'll see you at the next one.

