1
00:00:05,280 --> 00:00:10,230
Welcome back, everyone, in this lecture, we're going to discuss your names and the reverse function

2
00:00:10,230 --> 00:00:11,010
within Django.

3
00:00:12,450 --> 00:00:18,240
As our websites get larger and more complex, we're going to end up adding more and more views and URLs

4
00:00:18,540 --> 00:00:20,250
across the project and apps.

5
00:00:20,430 --> 00:00:25,020
This leads to more instances where we're going to need to reference some existing page on our web.

6
00:00:25,530 --> 00:00:32,640
For example, having a button that links to some other web page or URL path on some other web page across

7
00:00:32,640 --> 00:00:33,270
our website.

8
00:00:33,300 --> 00:00:38,250
So we keep linking across different pages, especially as the website gets larger and larger with more

9
00:00:38,250 --> 00:00:39,030
and more pages.

10
00:00:40,330 --> 00:00:43,120
Now, in this lecture, we're going to explore two key ideas.

11
00:00:43,450 --> 00:00:50,260
One is that you are a parts inside that path function can actually have names associated with them that

12
00:00:50,260 --> 00:00:54,310
can then be referenced across Jango and inside of a template.

13
00:00:54,640 --> 00:01:01,240
This is a super powerful tool, and it's going to make your code way more maintainable and way more

14
00:01:01,240 --> 00:01:01,900
robust.

15
00:01:02,110 --> 00:01:06,340
And it follows what's known as the dry principle, which is don't repeat yourself.

16
00:01:07,030 --> 00:01:12,310
Right now, we've have a few things that are hardcoded that are be much easier to actually refer to

17
00:01:12,310 --> 00:01:13,810
a name to a URL.

18
00:01:14,380 --> 00:01:20,050
Now, in order to use that with a view, Django also has a reverse function to find the corresponding

19
00:01:20,050 --> 00:01:22,540
URL path for a given URL name.

20
00:01:23,440 --> 00:01:29,740
So let me explain what the reverse function actually does through the use of this kind of thought experiment.

21
00:01:30,100 --> 00:01:34,000
What's the typical flow for a web page that we've seen so far in Django?

22
00:01:34,570 --> 00:01:41,410
Well, you have the user and that user types in some specific domain path for our URL and that says,

23
00:01:41,410 --> 00:01:45,700
Hey, I'm looking for this specific URL for first app.

24
00:01:45,700 --> 00:01:49,180
For sports, for instance, Django says no problem.

25
00:01:49,330 --> 00:01:54,340
Let me look that up across my URL patterns list, which is going to be in my URLs, not py file.

26
00:01:54,730 --> 00:01:58,810
Then, once it finds the match, it calls the corresponding view.

27
00:02:00,150 --> 00:02:06,780
Now, what if the request for the URL is actually happening within the Vieuxtemps Pie file and not directly

28
00:02:06,780 --> 00:02:07,710
from the user?

29
00:02:08,160 --> 00:02:13,770
Remember that as our website gets more and more complex, there's going to be instances where we want

30
00:02:13,770 --> 00:02:15,390
to redirect the user.

31
00:02:15,420 --> 00:02:20,910
Maybe they just filled out a form and we want to redirect them to some other web page once they completed

32
00:02:20,910 --> 00:02:21,510
that form.

33
00:02:21,960 --> 00:02:28,170
Or maybe we're trying to implement that physical numbering where we associate the number one to the

34
00:02:28,170 --> 00:02:33,960
top exports or a number two to finance, etc. So we already know we can have these sort of redirect

35
00:02:33,960 --> 00:02:40,170
views that taken the request and then say, Hey, I would like to redirect them to this other URL.

36
00:02:40,530 --> 00:02:44,820
And so far, we've technically been hard coding that other URL.

37
00:02:44,850 --> 00:02:49,410
It's not as clear right now because we don't really have templates in play, but this was technically

38
00:02:49,410 --> 00:02:50,160
hardcoded.

39
00:02:50,760 --> 00:02:59,160
What Jango has is the capability to not hard code that and instead simply refer to a URL path by name.

40
00:03:00,100 --> 00:03:03,460
So we end up doing is we have this reverse function.

41
00:03:03,970 --> 00:03:09,490
So what you do is you pass in the name of the URL and later on we'll see how to assign the name to a

42
00:03:09,500 --> 00:03:09,830
URL.

43
00:03:09,850 --> 00:03:15,160
It's actually quite easy simply say name is equal to inside that path function and then that gives you

44
00:03:15,160 --> 00:03:20,810
that web page or your URL object that you can then pass in for the HDP response.

45
00:03:20,830 --> 00:03:21,730
Redirect.

46
00:03:21,760 --> 00:03:27,580
So you'll end up calling the URL name inside a reverse and then pass that in to wherever you want to

47
00:03:27,580 --> 00:03:28,810
redirect the user.

48
00:03:29,140 --> 00:03:34,630
And typically, this is actually not done in two lines, you'll see the reverse call done inside ACP

49
00:03:34,780 --> 00:03:37,690
Response Redirect or HDP response call.

50
00:03:38,140 --> 00:03:42,730
Now, the reason it's called reverse is because you can think this is kind of happening in the reverse

51
00:03:42,730 --> 00:03:43,690
of what's normal.

52
00:03:44,110 --> 00:03:50,380
Normally, the URLs that PY file is looking for the view inside of used up high.

53
00:03:50,680 --> 00:03:57,370
But this time around, it's kind of the reverse where inside we've used our PI and reverse is asking,

54
00:03:57,370 --> 00:04:01,210
Hey, given this URL name, where's the corresponding URL?

55
00:04:01,570 --> 00:04:05,140
That's kind of where it gets its name is the fact that this is going in reverse.

56
00:04:05,140 --> 00:04:09,800
You're going from views to your URL instead of what's typical, which is here's the URL.

57
00:04:09,820 --> 00:04:11,290
Now get me the view for it.

58
00:04:11,500 --> 00:04:16,600
Instead, we're inside of views and then we're saying, Hey, what is actually happening inside URLs

59
00:04:16,839 --> 00:04:17,920
for this URL name?

60
00:04:19,079 --> 00:04:24,570
So let's go ahead and name a URL and then use that name inside a reverse call for Django.

61
00:04:25,230 --> 00:04:25,470
All right.

62
00:04:25,500 --> 00:04:26,070
Here I am.

63
00:04:26,070 --> 00:04:32,370
Inside of my application, I have used up high and I also have the URLs that pay for the application.

64
00:04:32,790 --> 00:04:39,720
What I'm going to do is on the path that actually takes in a string topic for the news view.

65
00:04:39,960 --> 00:04:44,280
Remember, that's the one that takes in something like forward slash finance or forward slash sports.

66
00:04:44,700 --> 00:04:49,650
I'm going to give this your URL a name, and let's go ahead and call it something like.

67
00:04:50,160 --> 00:04:50,760
Topic.

68
00:04:51,710 --> 00:05:00,200
Page now it's going to be super useful is now we can refer to this particular path inside of URL patterns

69
00:05:00,590 --> 00:05:02,150
by topic page.

70
00:05:02,510 --> 00:05:07,170
And you can even do that inside of your HTML template files.

71
00:05:07,190 --> 00:05:11,990
Later on, we actually learn how to syntax things with Django and ginger.

72
00:05:12,020 --> 00:05:18,470
So just this tool itself, the fact that you can name these paths inside URLs is going to make your

73
00:05:18,470 --> 00:05:23,930
life a thousand times easier and is a huge deal for what we're about to learn.

74
00:05:23,960 --> 00:05:28,670
I know it may seem really simple right now, but this is going to make your life a lot easier.

75
00:05:29,060 --> 00:05:30,350
So we're going to save that change.

76
00:05:30,770 --> 00:05:35,840
Notice, I'm just naming the topic one, and we're going to come back to views and take a look again

77
00:05:36,170 --> 00:05:37,970
at this number page view.

78
00:05:38,010 --> 00:05:43,040
Remember, this one was taking something that looked like this first, that poor size zero and then

79
00:05:43,040 --> 00:05:46,700
try to transform it to something like that first step forward slash sports.

80
00:05:47,730 --> 00:05:54,690
What are the issues right now is, let's say you revisit this and you have maybe a dozen more functions

81
00:05:54,690 --> 00:05:58,740
and they're all in different orders and you come to this function view and you're looking at it and

82
00:05:58,740 --> 00:06:02,310
you're trying to think, wait a minute, where is this actually redirecting to?

83
00:06:02,340 --> 00:06:06,570
I just said topic, but I have so many views now in my project.

84
00:06:06,570 --> 00:06:07,710
It's grown a lot more.

85
00:06:07,950 --> 00:06:09,330
I have so many URLs.

86
00:06:09,330 --> 00:06:13,830
I kind of have no idea what's actually happening by just putting in this random topic.

87
00:06:14,250 --> 00:06:20,400
So what would be really nice here is if instead I could say, Hey, I had this topic.

88
00:06:20,400 --> 00:06:27,690
I need you to do a reverse call for the topic page URL and then passed that topic into the path.

89
00:06:28,110 --> 00:06:29,340
What would it actually look like?

90
00:06:29,760 --> 00:06:35,010
You're going to see right now that upon revisiting this code after using reverse and then you are all

91
00:06:35,010 --> 00:06:38,520
names, it's going to be a lot more readable and a lot more maintainable.

92
00:06:39,060 --> 00:06:46,260
First thing you have to do is actually import reverse will say from Django that URLs import the reverse

93
00:06:46,260 --> 00:06:46,740
function.

94
00:06:47,520 --> 00:06:49,110
And then what we're going to do is down here.

95
00:06:49,290 --> 00:06:50,820
I'll do this first in two lines.

96
00:06:51,210 --> 00:06:57,840
So you can kind of see the change clearly is we're going to say that the web page we are redirecting

97
00:06:57,840 --> 00:06:58,170
to.

98
00:06:59,100 --> 00:07:00,420
Is going to say, hey.

99
00:07:01,390 --> 00:07:09,490
Do a reverse lookup here, essentially for the URL and the actual page I want you to match to is going

100
00:07:09,490 --> 00:07:13,360
to be called Topic Dash page.

101
00:07:13,990 --> 00:07:19,750
Now remember this topic page actually takes in a parameter and needs that topic, and sometimes certain

102
00:07:19,750 --> 00:07:22,120
parts are actually going to take in multiple parameters.

103
00:07:22,540 --> 00:07:28,570
For example, remember we had one path that we're actually able to add up those numbers like one forward

104
00:07:28,570 --> 00:07:30,280
slash to return back three.

105
00:07:30,790 --> 00:07:37,060
So it's going to end up happening is you have a second parameter here and it can take an args or keyword

106
00:07:37,060 --> 00:07:37,660
arguments.

107
00:07:38,050 --> 00:07:43,750
So we're going to say ARGS is equal to and the always pass the Senate as a list, even if it's just

108
00:07:43,750 --> 00:07:44,440
one item.

109
00:07:44,860 --> 00:07:52,210
And in this case, we're going to pass in topic as the argument to be passed in here for this particular

110
00:07:52,210 --> 00:07:53,230
argument for this path.

111
00:07:53,950 --> 00:07:54,310
OK.

112
00:07:54,760 --> 00:08:01,570
So then once we have that web page, I can simply put that here versus the topic.

113
00:08:02,320 --> 00:08:05,650
Now you typically won't see this be done in two lines.

114
00:08:05,650 --> 00:08:08,890
Instead, what's going to happen is you just cut this out.

115
00:08:09,990 --> 00:08:11,910
And then paste it in here.

116
00:08:12,300 --> 00:08:16,740
But before I do that, I want to just kind of do a quick overview what's actually happening here?

117
00:08:17,010 --> 00:08:21,120
And hopefully you can see why this is going to be more readable if somebody were to come back to this

118
00:08:21,390 --> 00:08:23,850
and say, Hey, what is some page of you doing?

119
00:08:24,180 --> 00:08:30,300
They can kind of see that it gets a topic and they say, Oh, well, it's passing the topic to this

120
00:08:30,300 --> 00:08:32,520
particular URL pathway.

121
00:08:32,850 --> 00:08:36,240
So then they can just say, OK, well, which one was called topic page?

122
00:08:36,630 --> 00:08:38,150
They would come to you, or I'll stop pie.

123
00:08:38,460 --> 00:08:41,080
And then they would just look for the name here at the end.

124
00:08:41,100 --> 00:08:42,539
And eventually, we all have names.

125
00:08:42,840 --> 00:08:48,420
You'll say, Oh, now I see the topic page that this is actually going back to, because the way this

126
00:08:48,420 --> 00:08:51,060
function was currently written was really quite weak.

127
00:08:51,300 --> 00:08:54,860
And it kind of only worked because we just had to URL paths.

128
00:08:54,870 --> 00:08:59,850
It would easily get broken more and more, especially as we expanded the number of views, a number

129
00:08:59,850 --> 00:09:00,630
of URLs.

130
00:09:00,780 --> 00:09:06,690
So right now, I have it in a much better position where I say, Hey, I'm looking for the Europe.

131
00:09:06,690 --> 00:09:10,830
Half that is called topic page and by the way, said that this topic.

132
00:09:10,950 --> 00:09:16,650
And that's where I want you to redirect to much cleaner, much easier to read and much better for maintenance.

133
00:09:16,980 --> 00:09:18,480
So we're actually going to then cut this out.

134
00:09:19,450 --> 00:09:23,590
And then paste that here, is that a Web page just to show you kind of the way you would typically see

135
00:09:23,590 --> 00:09:24,640
this in Django?

136
00:09:24,730 --> 00:09:28,600
So you have this reverse call in here topic page and then pass on the topic.

137
00:09:29,050 --> 00:09:30,520
So that's it.

138
00:09:30,550 --> 00:09:31,480
We're going to test this out.

139
00:09:31,480 --> 00:09:32,350
Make sure it's running.

140
00:09:33,040 --> 00:09:37,270
You technically won't see any changes from before, but let's go ahead and check it out anyway.

141
00:09:37,280 --> 00:09:44,740
So say a Python match run server, and then I'm going to bring in the Chrome browser where I have the

142
00:09:44,740 --> 00:09:45,280
page running.

143
00:09:45,670 --> 00:09:49,930
So here I am at home view and right now I'm going to say got a first app?

144
00:09:50,870 --> 00:09:57,650
Four zero hit enter, and it took me to the sports page, so it is doing the redirect, and let's try

145
00:09:57,650 --> 00:10:02,090
it one more time with, let's say, to enter, and that's taking to the politics page.

146
00:10:02,390 --> 00:10:02,900
Perfect.

147
00:10:03,290 --> 00:10:10,040
So again, what's happening here is you now have this amazing name that you can refer to for this entire

148
00:10:10,040 --> 00:10:10,460
path.

149
00:10:10,790 --> 00:10:14,990
And that's going to have functionality across templates across many different types of jingle files,

150
00:10:15,320 --> 00:10:19,700
and it has functionality within the views, especially when you call reverse here.

151
00:10:19,970 --> 00:10:25,250
And then you just say, Hey, reverse, look up that URL path and then send that these arguments so

152
00:10:25,460 --> 00:10:29,570
much clearer code and it's going to be a life saver for us later on in the course.

153
00:10:29,960 --> 00:10:31,850
OK, that's it for this lecture.

154
00:10:31,880 --> 00:10:36,680
I'll see if the next one where we actually connect this to a template instead of passing in just strings

155
00:10:36,680 --> 00:10:37,940
for each of your responses.

156
00:10:38,180 --> 00:10:38,720
I'll see you there.

