1
00:00:05,250 --> 00:00:09,340
Welcome back, everyone, to this lecture on using Django tags and URL names.

2
00:00:10,470 --> 00:00:16,379
Recall that we mentioned in the path function in the URLs, that pie file, we can actually assign names

3
00:00:16,379 --> 00:00:19,230
to specific web pages in our Django project.

4
00:00:19,710 --> 00:00:26,070
This, in turn, allows us to use the URL tag to easily create links to other pages based on their URL

5
00:00:26,070 --> 00:00:27,600
name in URLs, not pie.

6
00:00:27,900 --> 00:00:33,030
So now you don't need to worry about having to memorize the exact file path to any particular template.

7
00:00:33,270 --> 00:00:36,030
You can easily refer to them by the URL name.

8
00:00:36,300 --> 00:00:42,780
This is a hugely convenient tool and why we want to actually name our URLs inside the path function

9
00:00:42,960 --> 00:00:43,710
in the future.

10
00:00:44,130 --> 00:00:45,360
Let's explore how this works.

11
00:00:45,750 --> 00:00:49,830
OK, here we are back at the each HTML templates are working at earlier.

12
00:00:50,160 --> 00:00:56,080
I'm going to just clear this if tag and focus on the fact that we have two templates.

13
00:00:56,100 --> 00:01:02,120
One is the example each smell template and we can just go ahead and say template.

14
00:01:02,580 --> 00:01:05,910
Save that and then we have variable each HTML.

15
00:01:06,420 --> 00:01:09,180
Let's quickly review what their URL paths are.

16
00:01:09,900 --> 00:01:16,350
So if we go to URLs dot py, this is right now under my site to remember we've connected my app to include

17
00:01:16,350 --> 00:01:17,610
my app thought URLs.

18
00:01:17,940 --> 00:01:23,890
So we're going to go then to your all pie, which is inside my app and I have these two paths.

19
00:01:23,910 --> 00:01:30,750
One is the forward slash my app that goes straight to this example of you, which in turn in views.

20
00:01:30,750 --> 00:01:34,800
Recall this connects to example of you render example that each HTML.

21
00:01:35,520 --> 00:01:42,540
Then we have the variable path, which goes to variable view, which is just this variable here being

22
00:01:42,540 --> 00:01:42,990
sent.

23
00:01:42,990 --> 00:01:44,630
But the clear things and make it simpler.

24
00:01:44,640 --> 00:01:50,220
I'm going to delete this and we're really just focusing on there's not going to be any context past.

25
00:01:50,580 --> 00:01:54,840
But how do I connect this view to this view on the template side of things?

26
00:01:55,080 --> 00:01:56,170
Not a redirect.

27
00:01:56,170 --> 00:01:56,760
And so the view.

28
00:01:56,760 --> 00:02:00,840
But actually, it's the button that says, OK, you're an example of you go ahead and visit variable

29
00:02:00,840 --> 00:02:02,610
view, or at least our URLs.

30
00:02:03,150 --> 00:02:07,590
So we're going to do here is an R refresh my website.

31
00:02:08,100 --> 00:02:10,500
And if I just go to force my app.

32
00:02:11,540 --> 00:02:13,700
I have example, HTML template.

33
00:02:14,180 --> 00:02:16,460
Then if I go to forward slash.

34
00:02:17,450 --> 00:02:24,290
Variable that I have variable Timol, what I would like to do is to have a link somewhere on this site,

35
00:02:24,290 --> 00:02:28,520
or maybe I click some text here that links me to the variable.

36
00:02:28,850 --> 00:02:30,230
Dot h HTML template.

37
00:02:30,590 --> 00:02:32,900
So how can I do that using your URL names?

38
00:02:33,140 --> 00:02:33,560
Well.

39
00:02:34,490 --> 00:02:41,360
What I could do is the following I come to you URLs that pie, and my first step is to actually register

40
00:02:41,360 --> 00:02:42,020
with jingo.

41
00:02:42,260 --> 00:02:43,830
This app name.

42
00:02:43,850 --> 00:02:47,660
And typically this is going to match the app name of the folder.

43
00:02:47,660 --> 00:02:50,300
You decided just to make sure that it doesn't get too confusing.

44
00:02:50,780 --> 00:03:00,500
So we come back here and we say app underscore name is equal to and the string my underscore app.

45
00:03:01,100 --> 00:03:03,590
This is just the general notation.

46
00:03:03,590 --> 00:03:09,680
So typically by convention, you're going to have the string match your actual application name so they

47
00:03:09,680 --> 00:03:11,120
will go ahead and save that change.

48
00:03:11,420 --> 00:03:16,270
Keep in mind, app name this is actually a special variable jingle looks for inside of your octopi.

49
00:03:16,520 --> 00:03:18,620
You have to call it app underscore name.

50
00:03:19,250 --> 00:03:24,410
OK, so we have the app underscore name variable, and we've now defined it inside URLs that pie.

51
00:03:24,770 --> 00:03:29,810
And this is essentially going to register the app namespace.

52
00:03:30,820 --> 00:03:33,550
Which is going to be used for the URL names.

53
00:03:34,000 --> 00:03:37,840
So that's step one, the find the app name inside of URLs that pie.

54
00:03:38,410 --> 00:03:43,510
Let's now move on to step two, which is actually give these paths names that we can use.

55
00:03:44,110 --> 00:03:51,130
So, for example, I can give this path the name example because I salute the example of you.

56
00:03:51,580 --> 00:03:53,770
And then I can give this path the name.

57
00:03:54,190 --> 00:03:55,060
And let's just call it.

58
00:03:56,520 --> 00:04:03,120
Variable, and I'm going to save those two, so I have to URL paths, one is example, one is variable

59
00:04:03,120 --> 00:04:07,560
and they are located underneath the URLs folder, which has been registered to have the app name.

60
00:04:07,800 --> 00:04:08,430
My app.

61
00:04:08,790 --> 00:04:14,640
Now the question is how we actually use these names and inform Django on the template side of things

62
00:04:14,820 --> 00:04:16,410
that they connect to a particular view.

63
00:04:16,980 --> 00:04:23,040
Well, I'm going to come back to example the HTML, and I'm going to create an A. tag that actually

64
00:04:23,040 --> 00:04:23,670
links to this.

65
00:04:23,970 --> 00:04:28,610
So underneath that example is HTML template.

66
00:04:28,620 --> 00:04:29,910
I'm going to create a new heading.

67
00:04:32,160 --> 00:04:35,040
And let's call it the following let's make an anchor tag here.

68
00:04:36,220 --> 00:04:39,640
And then we're going to say each ref is equal to.

69
00:04:40,180 --> 00:04:43,660
And then inside a double quote string.

70
00:04:44,140 --> 00:04:49,030
Typically, when you'd be creating this sort of reference, it'd be kind of annoying because we would

71
00:04:49,030 --> 00:04:56,140
have to say something like C four slash users, Florida, slash my username and Django, et cetera,

72
00:04:56,140 --> 00:04:58,960
all the way until I got to something like.

73
00:04:59,840 --> 00:05:05,060
Variable HTML now, clearly, we don't want to have to write something as complicated as this.

74
00:05:05,360 --> 00:05:10,910
So instead of writing all that, we end up using a URL tag, so we say curly braces.

75
00:05:11,920 --> 00:05:16,450
Percent, nine percent sign curly braces and then inside of here, I use the key word.

76
00:05:16,600 --> 00:05:17,620
You are L.

77
00:05:18,690 --> 00:05:19,350
Space.

78
00:05:20,380 --> 00:05:25,310
And then inside here, I'm going to have some strings that actually connect to the URL name.

79
00:05:25,330 --> 00:05:29,800
And the first thing that goes in is the application name my underscore app.

80
00:05:30,400 --> 00:05:34,510
And then you say colon and whatever your view you're actually interested in.

81
00:05:34,960 --> 00:05:40,750
So right now, this is the example that HTML and I would like to have a button here that says, go ahead

82
00:05:40,750 --> 00:05:46,420
and click on me and go to variable that HTML so that your name is called variable.

83
00:05:46,900 --> 00:05:48,310
So we'll come back here and say.

84
00:05:50,380 --> 00:05:57,160
Variable and then we do have to have the closing of that anchor tag and then some sort of button, so

85
00:05:57,610 --> 00:06:00,070
we'll say click me to go to.

86
00:06:01,250 --> 00:06:04,250
Variable and then we will close off that increase that.

87
00:06:07,680 --> 00:06:10,440
OK, so pay close attention to what's happening here.

88
00:06:10,740 --> 00:06:12,390
I have inside heading one.

89
00:06:12,810 --> 00:06:19,080
This anchor tag and the ref for the anchor tag instead of being some long, complicated pathway to a

90
00:06:19,080 --> 00:06:20,610
specific HTML file.

91
00:06:20,850 --> 00:06:23,400
I'm now taking advantage of two things.

92
00:06:23,610 --> 00:06:31,650
One is a URL tag and the two is the capability to actually then use the URL names, as well as the newly

93
00:06:31,650 --> 00:06:34,110
defined app name to easily connect this.

94
00:06:34,110 --> 00:06:37,920
And then I can reference this sort of thing anywhere on my website.

95
00:06:38,430 --> 00:06:39,420
So I'm going to save that.

96
00:06:40,720 --> 00:06:44,320
And let's just see what happens right now if we try to run this as it is.

97
00:06:45,010 --> 00:06:50,380
So I'm going to come back here and, ah, refresh the page and it says, Hey, click me to go to variable

98
00:06:50,380 --> 00:06:52,990
so we can see the anchor text there if I click this.

99
00:06:53,050 --> 00:06:55,930
Now it takes me to variable thought HTML.

100
00:06:56,590 --> 00:07:02,830
So what's happening here is jingo is now alerting it to the fact that underneath my app, go to that

101
00:07:02,830 --> 00:07:08,770
URLs that pie and then search for the URL path that has the name variable and then serve that view.

102
00:07:09,160 --> 00:07:15,130
So now you can see why it's so useful to begin naming inside your old patterns the different view path

103
00:07:15,130 --> 00:07:15,790
connections.

104
00:07:16,120 --> 00:07:20,760
That way, when you're dealing with the templates, it becomes super easy to reference things without

105
00:07:20,770 --> 00:07:23,500
having to remember where exactly they are located.

106
00:07:24,340 --> 00:07:31,450
Now, another thing to keep in mind is let's say you had some templates or some views or URLs that were

107
00:07:31,450 --> 00:07:33,500
not associated with any particular application.

108
00:07:33,520 --> 00:07:39,670
So if I open up explorer, really what I'm talking about is if for some reason you have your URLs on

109
00:07:39,670 --> 00:07:44,330
a site level that you actually want to reference if you want to do that.

110
00:07:44,350 --> 00:07:51,250
For example, you have some path here that leads to maybe your home page for your entire website, and

111
00:07:51,250 --> 00:07:54,430
it's actually not associated with any particular extension.

112
00:07:54,850 --> 00:08:00,190
All you need to do is if you have some views that are at my site level instead of at a particular application

113
00:08:00,190 --> 00:08:07,490
level, you would just come back to here and remove my app and say something like variable.

114
00:08:07,810 --> 00:08:12,760
This would be if the variable existed as a template on a project site level, but typically it's going

115
00:08:12,760 --> 00:08:15,160
to be a lot easier to organize things by application.

116
00:08:15,400 --> 00:08:22,150
And you can even organize your home page, so to say no extension based off some other app within your

117
00:08:22,150 --> 00:08:22,630
project.

118
00:08:22,780 --> 00:08:29,350
So what you mean by that is if you were to come here to your URLs, you would just define some pathway

119
00:08:29,350 --> 00:08:31,000
here that was actually blank.

120
00:08:31,120 --> 00:08:33,700
And then you would include something like home that URL.

121
00:08:33,700 --> 00:08:39,010
So you'd actually end up just making a new Django application for all your kind of home based stuff

122
00:08:39,250 --> 00:08:40,360
and simply connect it here.

123
00:08:40,750 --> 00:08:41,460
It's kind of up to you.

124
00:08:41,470 --> 00:08:43,030
Some people like to do it at a project level.

125
00:08:43,270 --> 00:08:46,360
Some people like separate into kind of some homepage application.

126
00:08:46,720 --> 00:08:47,410
It's kind of up to you.

127
00:08:47,410 --> 00:08:50,410
But again, what's the main thing we learned right now?

128
00:08:50,590 --> 00:08:57,610
The fact that all I need to do is step one give my application URLs, stop, file a registered app namespace,

129
00:08:57,970 --> 00:09:02,590
then create a name for a particular URL pattern path connected to a view.

130
00:09:02,890 --> 00:09:09,140
And that means in one of these templates, I can reference it and you don't need to reference it specifically

131
00:09:09,140 --> 00:09:09,720
to make or tag.

132
00:09:09,730 --> 00:09:11,080
But this is the most common way to do it.

133
00:09:11,380 --> 00:09:17,350
Where you use the URL tag my app variable and this tag by itself is essentially OK.

134
00:09:17,620 --> 00:09:21,130
Here's a direct link to go to this URL connected to this view.

135
00:09:21,400 --> 00:09:22,900
So super, super useful.

136
00:09:23,080 --> 00:09:27,100
We're going to be using this a lot, especially on something like a navigation bar at the top of the

137
00:09:27,100 --> 00:09:27,520
page.

138
00:09:28,090 --> 00:09:32,650
Speaking of which, let's go ahead and learn how to inherits from other HTML templates.

139
00:09:32,920 --> 00:09:33,850
I'll see you at the next lecture.

