1
00:00:05,150 --> 00:00:06,140
Welcome back, everyone.

2
00:00:06,200 --> 00:00:09,290
And this lecture will be discussing dynamic views and logic.

3
00:00:10,530 --> 00:00:12,750
We're now going to explore two key ideas.

4
00:00:13,140 --> 00:00:19,020
One is just adding in simple logic to a view that is connecting Python code rather than just send back

5
00:00:19,050 --> 00:00:21,270
a string through an carp response.

6
00:00:21,630 --> 00:00:26,730
And the second one is a very important idea, which is creating dynamic paths that is having that actual

7
00:00:26,730 --> 00:00:29,700
Yoro Path route defined by the client.

8
00:00:30,030 --> 00:00:32,340
Let's check this all out in Visual Studio.

9
00:00:33,090 --> 00:00:33,330
All right.

10
00:00:33,360 --> 00:00:40,350
Here I am back at our code right now we have one simple view and that's connected to URLs patterns or

11
00:00:40,350 --> 00:00:42,960
you're pie inside the application here.

12
00:00:42,960 --> 00:00:49,740
Path views that simple view and then that is connected to our project level here under path first app

13
00:00:49,740 --> 00:00:51,570
include first app URLs.

14
00:00:51,930 --> 00:00:57,130
So if I were to go to my domain name forward slash first app, I get the actual result.

15
00:00:57,150 --> 00:00:58,620
Simple view here, my browser.

16
00:00:59,190 --> 00:01:05,730
Now let's imagine we've been inspired by the newsroom sources of Django, so the fact that it started

17
00:01:05,730 --> 00:01:12,390
in NEWSROOM and I decide that I'm going to create a newspaper online, so I'm going to make actual several

18
00:01:12,390 --> 00:01:12,780
views.

19
00:01:13,290 --> 00:01:17,340
Let's make one for the sports page, so we'll have a sports view.

20
00:01:17,370 --> 00:01:21,150
And then this one's just going to say something like sports page.

21
00:01:22,200 --> 00:01:27,270
And then what I'm also going to do is create another view, and I'm going to call this view, let's

22
00:01:27,270 --> 00:01:32,130
say our finance view for financial news, so have this won't be a request as well.

23
00:01:33,060 --> 00:01:36,790
And then that's going to send an HTP response.

24
00:01:36,810 --> 00:01:39,030
And this is going to say something like finance page.

25
00:01:40,910 --> 00:01:41,240
All right.

26
00:01:41,750 --> 00:01:46,640
So we have a sports view for a sports page and a finance view for the finance page.

27
00:01:47,030 --> 00:01:51,260
And what I need to do now is actually connect these multiple views to a URL.

28
00:01:51,710 --> 00:01:57,620
So I'll come back here and we'll have one go ahead and be sports for us, and we'll connect that to

29
00:01:57,620 --> 00:01:58,700
the sports view.

30
00:01:59,180 --> 00:02:01,310
And then I'll add in another path connection.

31
00:02:02,280 --> 00:02:08,460
And this one's going to be finance forward slash, and we'll connect that two views Dot and then we

32
00:02:08,460 --> 00:02:14,420
have a finance view if I come back here or call it, have finance for you, so we'll connect it to finance

33
00:02:14,450 --> 00:02:14,670
you.

34
00:02:16,130 --> 00:02:16,670
There we go.

35
00:02:17,000 --> 00:02:25,160
So I can save that change and then I'll come back here and actually if I refresh this now, I'll get

36
00:02:25,160 --> 00:02:29,900
nothing found since not only have admin, first up, sports, first up finance.

37
00:02:30,380 --> 00:02:31,130
So let's check that out.

38
00:02:31,310 --> 00:02:31,890
I'll go first.

39
00:02:31,920 --> 00:02:34,880
App Sports and I see the sports page.

40
00:02:35,300 --> 00:02:39,230
I should see the same for finance hit enter and then I have that as well.

41
00:02:40,280 --> 00:02:44,060
Now, one issue with this is for every new topic I want to add.

42
00:02:44,360 --> 00:02:48,860
I have to create a new view and then create a corresponding new URL.

43
00:02:49,520 --> 00:02:51,560
Let's see if we can be a little smarter about this.

44
00:02:52,130 --> 00:02:57,530
For instance, one thing I can do is take advantage of Python code, so either by importing another

45
00:02:57,530 --> 00:03:00,380
Python file or by writing this itself inside of.

46
00:03:01,100 --> 00:03:04,490
Maybe I'll just have something like the articles.

47
00:03:05,710 --> 00:03:09,190
Be formatted in a dictionary and then I'll do something like this.

48
00:03:09,220 --> 00:03:09,940
Sports.

49
00:03:11,070 --> 00:03:13,200
And I'll have this be the sports page.

50
00:03:14,690 --> 00:03:19,610
Comma, and then here, I'll say finance and I'll have this return.

51
00:03:21,190 --> 00:03:23,330
Finance page comma.

52
00:03:24,070 --> 00:03:30,040
And then let's have one more, let's say, for politics, and I'll have this one return back the politics

53
00:03:30,220 --> 00:03:30,670
page.

54
00:03:31,710 --> 00:03:36,420
And then I could try being a little smarter here and then say it's the poor response instead of just

55
00:03:36,420 --> 00:03:37,620
typing in sports page.

56
00:03:38,070 --> 00:03:39,660
I'll say something like articles.

57
00:03:40,650 --> 00:03:44,340
And then grab sports and then here I could say articles.

58
00:03:45,910 --> 00:03:49,350
And then grab everything to do with the finance key.

59
00:03:50,590 --> 00:03:55,360
So the one thing I want to get across right now is while we probably wouldn't do this in a real website

60
00:03:55,630 --> 00:04:01,660
is the fact that I'm starting to mix in python objects like a dictionary to my views.

61
00:04:02,170 --> 00:04:08,410
And if I were to come back here and start refreshing this, you'll notice that finance is working OK?

62
00:04:09,540 --> 00:04:15,030
Sports is also working, OK, but remember I right now they'll actually have a view on your connection

63
00:04:15,030 --> 00:04:24,000
for politics, which means if I come here and type out first app slash politics, then it's not working.

64
00:04:24,630 --> 00:04:30,390
Now, clearly, as we've mentioned before, it'd be nice if I was able to dynamically update this.

65
00:04:30,390 --> 00:04:35,040
That is not to define every single possible type of view here.

66
00:04:35,040 --> 00:04:41,160
Instead, just to find something in a Python object and later have that sync up based off what I'm actually

67
00:04:41,160 --> 00:04:41,850
looking for.

68
00:04:42,150 --> 00:04:43,380
So how can I do that?

69
00:04:44,880 --> 00:04:47,880
We can use dynamic routing in order to create this.

70
00:04:48,480 --> 00:04:57,210
So I'm going to close this off or delete this and create a single view called News View, and it won't

71
00:04:57,210 --> 00:05:02,010
just take in a request, but it's also going to take in a topic.

72
00:05:02,610 --> 00:05:11,100
And this way, I can do the following I can say that my topic is going to be what I pass in here in

73
00:05:11,100 --> 00:05:13,320
order to actually get an HDP response.

74
00:05:13,320 --> 00:05:18,000
So I pass on a topic so it could be sports, finance or politics.

75
00:05:18,330 --> 00:05:21,330
And then that's going to retrieve and create the news view.

76
00:05:21,900 --> 00:05:27,390
Now I do have one issue is the fact that I technically do not know this topic ahead of time.

77
00:05:27,810 --> 00:05:30,420
The user or client is going to type that in.

78
00:05:30,960 --> 00:05:36,990
It's going to type in the domain for its first step forward slash and then some sort of article topic.

79
00:05:37,380 --> 00:05:43,920
So what I need to do is make Django aware that this topic needs to also be dynamically form for the

80
00:05:43,920 --> 00:05:44,760
URL pattern.

81
00:05:45,180 --> 00:05:47,310
And luckily, Django makes that quite easy.

82
00:05:48,190 --> 00:05:54,250
We come to Earl's dot pie and then instead of defining a path for every single possible article type,

83
00:05:54,580 --> 00:05:59,980
we're just going to say the following I'll say path and then the way we actually tell Django that this

84
00:05:59,980 --> 00:06:06,460
is going to be dynamically defined by what's actually passed in to the view here for a topic is with

85
00:06:06,460 --> 00:06:07,510
the following syntax.

86
00:06:08,170 --> 00:06:14,200
We use these two races or brackets that is less than and greater than sine.

87
00:06:14,560 --> 00:06:20,650
And then I can pass in topic and connect that to views that.

88
00:06:22,130 --> 00:06:24,860
News view, and then we want this to be a topic.

89
00:06:25,730 --> 00:06:26,600
Forward slash.

90
00:06:26,870 --> 00:06:27,410
There we go.

91
00:06:28,280 --> 00:06:34,790
And so now what this is saying is the user can pass in whatever they want into the news, view whatever

92
00:06:34,790 --> 00:06:35,990
topic they're looking for.

93
00:06:36,290 --> 00:06:38,930
We're going to look it up sports, finance or politics.

94
00:06:39,260 --> 00:06:45,500
And then we're also going to define that dynamically in your old patterns as topic so I can come back

95
00:06:45,500 --> 00:06:45,860
here.

96
00:06:46,580 --> 00:06:48,690
Recall politics used to not be found.

97
00:06:48,710 --> 00:06:51,800
I'm going to refresh this and now I see the politics page.

98
00:06:52,340 --> 00:06:56,270
And if I try sports enter, I now see the sports page.

99
00:06:56,720 --> 00:07:02,030
Now we still have the issue that if something doesn't exist, so we currently don't have a section or

100
00:07:02,030 --> 00:07:03,680
a website for, let's say, the arts.

101
00:07:04,100 --> 00:07:08,840
So if I type in first app for arts enter, then I get this error.

102
00:07:08,870 --> 00:07:10,150
Hey, there was nothing there for arts.

103
00:07:10,160 --> 00:07:10,820
So later on.

104
00:07:11,060 --> 00:07:16,430
We're probably going to want to use some sort of Easter to be response to redirect them in case we're

105
00:07:16,430 --> 00:07:17,360
looking for something.

106
00:07:17,540 --> 00:07:21,200
And right now, you'll notice we actually get a key error that's indicating that the error is actually

107
00:07:21,200 --> 00:07:25,340
happening on the dictionary side of things, not really on the jingo side of things, although it's

108
00:07:25,340 --> 00:07:26,540
technically all connected.

109
00:07:27,170 --> 00:07:33,590
But what I do want to show you to finish off this idea of the integrated URL patterns is the fact that

110
00:07:33,920 --> 00:07:42,230
I can pass in this dynamic variable and use it both on the Python view side of things and dynamically

111
00:07:42,230 --> 00:07:43,880
update the URL patterns.

112
00:07:44,510 --> 00:07:51,110
The last thing I can also do is what's known as a path converter, so you can use a path converter to

113
00:07:51,110 --> 00:07:57,500
specify the Django that you want a particular variable to be treated, let's say, as a string or as

114
00:07:57,500 --> 00:08:02,600
an integer, and you can check out all different path converters on the Django documentation.

115
00:08:04,690 --> 00:08:10,690
We'll talk about these in more detail later on, but if you go to the documentation on the URL dispatcher,

116
00:08:11,020 --> 00:08:13,810
you can see there will be a section on path converters.

117
00:08:14,080 --> 00:08:19,180
And you can actually pass this in a saying make sure it's a string or make sure it's an integer, a

118
00:08:19,180 --> 00:08:19,650
slug.

119
00:08:19,660 --> 00:08:23,980
We'll talk about that later on, but it basically puts in these dashes into longer strings.

120
00:08:24,220 --> 00:08:26,830
It can be a unique ID or can just be path.

121
00:08:27,220 --> 00:08:29,230
So we'll talk about path converters later on.

122
00:08:29,230 --> 00:08:33,640
But I do want to show you their power, essentially informing Django, Hey, it's going to be a string

123
00:08:33,640 --> 00:08:34,690
or it's going to be an integer.

124
00:08:35,169 --> 00:08:41,590
So something here that we won't actually see the effect of, but I could inside those brackets percent

125
00:08:41,630 --> 00:08:47,680
SDR colon and this will make sure that topics is going to be a path converter for a string.

126
00:08:48,250 --> 00:08:53,290
To show you another example of this, we're going to do something slightly more complex to finish off

127
00:08:53,290 --> 00:08:57,970
this lecture, so I won't come to views that PI and I'm going to create a view.

128
00:08:59,010 --> 00:08:59,920
Cardiff.

129
00:09:01,240 --> 00:09:05,380
That view takes in a request and then takes in two parameters.

130
00:09:05,710 --> 00:09:07,720
Number one and number two.

131
00:09:09,050 --> 00:09:16,070
And then what I'm going to do is I'm actually going to calculate the result of adding number one plus

132
00:09:16,070 --> 00:09:18,680
number two, and these are going to be treated as integers.

133
00:09:19,040 --> 00:09:27,230
Keep in mind, I need two percent integers here and then I'm going to return an HDP response of the

134
00:09:27,230 --> 00:09:29,570
string result of that results here.

135
00:09:30,050 --> 00:09:36,770
So what I'm going to try to do is the following I want the user to be able to visit my domain and underneath

136
00:09:36,780 --> 00:09:44,240
first app, they can type in number one, forward slash number two, and then the result is actually

137
00:09:44,240 --> 00:09:49,700
going to be number one plus number two, just to give you an idea of how dynamic this can be.

138
00:09:49,970 --> 00:09:56,510
So, for example, if the user goes to first app and then type in three forward slash four, then the

139
00:09:56,510 --> 00:09:58,940
result on the web page should be seven.

140
00:09:59,570 --> 00:10:01,400
So actually adding three and four.

141
00:10:02,090 --> 00:10:07,160
But to do that, I need to make sure that Jeong understands these are integers, so I'm going to save

142
00:10:07,400 --> 00:10:08,090
Advil.

143
00:10:08,510 --> 00:10:10,430
And then let's add it in dynamically.

144
00:10:10,680 --> 00:10:13,030
So we're going to say comma here path.

145
00:10:14,120 --> 00:10:16,700
And then I'm going to define integer.

146
00:10:17,810 --> 00:10:18,530
No one.

147
00:10:21,220 --> 00:10:29,590
Bracket and then forward slash integer colon, and then we're going to say no to bracket and we have

148
00:10:29,590 --> 00:10:31,900
to actually make sure this all goes within the same string.

149
00:10:33,060 --> 00:10:42,330
So let's delete this quote here, and you can see this dynamic routing is essentially what I was defining

150
00:10:42,360 --> 00:10:42,720
here.

151
00:10:42,810 --> 00:10:45,900
Three comma four or number one.

152
00:10:46,710 --> 00:10:49,320
Number two, that it actually means they come at forward slash.

153
00:10:49,680 --> 00:10:52,560
So we have first that forward slash number one for System two.

154
00:10:53,280 --> 00:10:54,420
And you can see here we have.

155
00:10:55,460 --> 00:11:02,510
Number one, number two, we just need to connect the view, so say views, dot and then this ad view.

156
00:11:04,460 --> 00:11:10,970
OK, so there we have it, let's see if this work is going to come back here and let's go first app

157
00:11:11,660 --> 00:11:18,770
and let's try it first app forward slash three forward slash four enter and you can see I get seven

158
00:11:19,160 --> 00:11:20,450
just to make it really clear.

159
00:11:20,450 --> 00:11:22,610
What I could also do is the following.

160
00:11:23,610 --> 00:11:31,440
I could say that my result is actually equal to the let's actually say this as a result.

161
00:11:32,930 --> 00:11:34,490
So I'll say this is the end result.

162
00:11:36,820 --> 00:11:38,890
And then I could say the following here.

163
00:11:39,810 --> 00:11:42,150
I can make this a string, and I could say.

164
00:11:43,430 --> 00:11:44,210
No one.

165
00:11:45,340 --> 00:11:45,940
Plus.

166
00:11:47,140 --> 00:11:48,010
Numb to.

167
00:11:49,220 --> 00:11:55,640
Equals and then just pass in as a result, and that's going to be some formatting there.

168
00:11:56,150 --> 00:11:59,180
So let's save that change and if I refresh this.

169
00:12:00,550 --> 00:12:03,070
I shall now see three plus four is equal to seven.

170
00:12:03,100 --> 00:12:04,630
And you can experiment with these.

171
00:12:04,840 --> 00:12:09,460
Obviously, it expects this to be integers, but you get the idea 10 plus 12 equals twenty.

172
00:12:10,210 --> 00:12:16,390
So hopefully that gives you an idea of the power of dynamically updating these views and starting to

173
00:12:16,390 --> 00:12:19,210
connect them with the idea of just normal Python objects.

174
00:12:19,570 --> 00:12:19,930
OK.

175
00:12:20,410 --> 00:12:22,750
So let's quickly review what we've done so far.

176
00:12:23,080 --> 00:12:29,050
We understood that I can start integrating the idea of different python objects within views and start

177
00:12:29,050 --> 00:12:31,270
using that Python code and logic.

178
00:12:31,480 --> 00:12:37,840
And then the other thing now I understand is the fact that I can have user dynamically updated routing

179
00:12:37,840 --> 00:12:38,290
and views.

180
00:12:38,290 --> 00:12:44,710
So I pass on topic here, and I just need to understand that using these brackets of less than greater

181
00:12:44,710 --> 00:12:48,250
than science is the way I can update paths dynamically.

182
00:12:48,670 --> 00:12:49,930
So that's the main thing is to get from.

183
00:12:49,930 --> 00:12:52,660
This lessens the fact he can update those paths dynamically.

184
00:12:53,110 --> 00:12:57,350
However, we still have the issue of what happens if that page doesn't exist.

185
00:12:57,370 --> 00:12:57,940
What do we do?

186
00:12:58,330 --> 00:13:01,870
We're going to cover that with four or fours and redirects in the next lecture.

187
00:13:02,110 --> 00:13:02,650
I'll see you there.

