1
00:00:05,220 --> 00:00:09,720
Welcome back, everyone, to the final topic for the section, which is connecting to templates, which

2
00:00:09,720 --> 00:00:15,270
will lead us into the next section where we have a much deeper dive into templates and syntax for that.

3
00:00:16,239 --> 00:00:22,510
So realistically, we don't want to have to manually type out each HTML code or HDP responses, insider

4
00:00:22,510 --> 00:00:24,610
views that pie file instead.

5
00:00:24,820 --> 00:00:31,180
We would like to separate out all our templates that has all those HTML files into a separate directory

6
00:00:31,180 --> 00:00:35,920
and then have the views be able to communicate between this directory and render the templates.

7
00:00:36,040 --> 00:00:40,780
So it's kind of a two way street because we want to be able to understand where the actual HTML files

8
00:00:40,780 --> 00:00:41,020
are.

9
00:00:41,230 --> 00:00:45,910
And then we also want to be able to insert information into those template files.

10
00:00:47,040 --> 00:00:53,130
Now connecting to a template directory does require us to inform the Django project settings where to

11
00:00:53,130 --> 00:00:58,020
find these templates, and in this simple example, we're actually going to store all the templates

12
00:00:58,020 --> 00:00:59,340
at a project level.

13
00:00:59,640 --> 00:01:04,590
But keep in mind, this is actually kind of an unusual way to do things is just for demonstration purposes

14
00:01:04,590 --> 00:01:05,069
only.

15
00:01:05,280 --> 00:01:10,440
Later on in the next section, the course will actually explore how to store templates on a Django application

16
00:01:10,440 --> 00:01:13,980
level, which is typically the recommended way of doing things that way.

17
00:01:14,010 --> 00:01:19,800
All your templates associated with an application are grouped together inside that application folder,

18
00:01:19,980 --> 00:01:24,210
but for right now, we're going to explore a little bit of how Django Settings actually works.

19
00:01:25,380 --> 00:01:29,880
So in this lecture, we're going to create a new templates directory, we'll do it at the project level

20
00:01:29,880 --> 00:01:30,180
again.

21
00:01:30,180 --> 00:01:32,280
Later on, we'll learn how to do it at an application level.

22
00:01:32,670 --> 00:01:38,040
We'll create an example each HTML file, then we'll connect that each HTML file to a view.

23
00:01:38,400 --> 00:01:42,900
And then we're going to explore how to actually inform the Django project, where to find this template

24
00:01:42,900 --> 00:01:45,780
directory inside of its settings that pie.

25
00:01:46,170 --> 00:01:49,020
So let's head to Visual Studio code and get started.

26
00:01:49,260 --> 00:01:50,010
OK, here I am.

27
00:01:50,310 --> 00:01:56,130
Views the pie that we've already been playing around with, as well as URLs that pie inside our first

28
00:01:56,130 --> 00:01:58,720
underscore app to simplify everything.

29
00:01:58,740 --> 00:02:06,090
I'm going to delete all the views that we have so far, as well as this article's dictionary, and I'm

30
00:02:06,090 --> 00:02:08,100
going to remove those URL patterns.

31
00:02:09,449 --> 00:02:14,670
And let's kind of start from scratch showing you how to connect a URL to a view then to a template.

32
00:02:15,560 --> 00:02:17,300
So what I'm going to do here is simply say path.

33
00:02:18,140 --> 00:02:24,980
I'll set this as empty and then I'm going to say views dot simple underscore view, which means I need

34
00:02:24,980 --> 00:02:28,280
to find a function called simple view inside of used up pie.

35
00:02:28,370 --> 00:02:34,130
I'm going to save that change and recall this is essentially going to our domain dot com forward slash

36
00:02:34,130 --> 00:02:34,910
first app.

37
00:02:34,910 --> 00:02:36,950
What view that we actually want to show here?

38
00:02:37,250 --> 00:02:38,840
That's going to be the simple view function.

39
00:02:39,230 --> 00:02:40,250
So I save that change.

40
00:02:40,700 --> 00:02:43,490
Go back to the user pie and let's actually create that simple view.

41
00:02:44,090 --> 00:02:46,460
Note this needs to match exactly how it was written.

42
00:02:47,000 --> 00:02:48,920
So this is going to take in a request.

43
00:02:50,290 --> 00:02:55,780
And then what we're going to do is we are going to use the render function and we have to return this.

44
00:02:56,200 --> 00:03:02,470
So we're going to say return the render and this is where you import from Django that shortcuts import,

45
00:03:02,470 --> 00:03:08,590
render and render can take in a bunch of arguments, but it's mainly just the HDP request, the template

46
00:03:08,590 --> 00:03:10,000
name and then the context.

47
00:03:10,300 --> 00:03:16,630
We'll talk a lot more about context later on, but context is what is passed back to that actual template

48
00:03:16,630 --> 00:03:17,080
file.

49
00:03:17,500 --> 00:03:19,450
So the request is just a request.

50
00:03:19,450 --> 00:03:25,420
I was passing into simple view and then the actual template name is something we have to decide.

51
00:03:25,900 --> 00:03:30,250
So I'm going to leave that blank for now because we're going to explore that in just a second.

52
00:03:30,820 --> 00:03:38,050
So what I need to do is actually say, Hey, there's some HTML file somewhere inside my directories,

53
00:03:38,320 --> 00:03:39,790
where should I actually point it to?

54
00:03:40,270 --> 00:03:43,090
So we're going to expand our explore here.

55
00:03:43,510 --> 00:03:47,770
This is where you have to be really careful, where you actually create in this ongoing collapse first

56
00:03:47,770 --> 00:03:50,380
app and collapse my site.

57
00:03:50,770 --> 00:03:56,050
And what I'm going to do is in the exact same directory that managed is in.

58
00:03:56,110 --> 00:04:00,760
So the very top my site, I'm going to create a new folder.

59
00:04:02,090 --> 00:04:06,080
And we are going to call it templates has to be this name templates.

60
00:04:06,680 --> 00:04:12,410
And again, this is at a project level kind of unusual to do it this way that an application template

61
00:04:12,410 --> 00:04:13,820
lives all the way at the project level.

62
00:04:14,060 --> 00:04:18,050
But just keep in mind, is kind of the simple way to do it before we actually learn about migrations.

63
00:04:18,950 --> 00:04:24,110
Next, what I'm going to do is inside of templates for a little bit of organization or create a new

64
00:04:24,110 --> 00:04:31,040
folder that will match the name of my application, which is first app and then inside a first app,

65
00:04:31,460 --> 00:04:32,780
I'll create a new file.

66
00:04:33,740 --> 00:04:34,400
Which will be.

67
00:04:35,380 --> 00:04:37,690
Called example thought each HTML.

68
00:04:38,320 --> 00:04:44,200
So note where this is living, it's under my site templates first app example HTML.

69
00:04:44,560 --> 00:04:50,630
It's not underneath this my site with settings, not pie, URLs, views, et cetera.

70
00:04:50,680 --> 00:04:54,330
Instead, it's living all the way at the manage that pie.

71
00:04:54,340 --> 00:04:56,590
So the very top level I create as templates.

72
00:04:56,920 --> 00:04:58,600
First example HTML.

73
00:04:58,930 --> 00:05:00,280
Later on, we'll do things this way.

74
00:05:00,280 --> 00:05:04,150
But right now, this kind of simplest way to actually render a template.

75
00:05:04,930 --> 00:05:11,110
So you should be able to type out doc, and then it will automatically fill in this HTML templates and

76
00:05:11,110 --> 00:05:16,210
let's create an H1 header that just says something like example each HTML.

77
00:05:17,220 --> 00:05:17,820
Connected.

78
00:05:18,740 --> 00:05:19,790
Save that change.

79
00:05:20,000 --> 00:05:27,380
And now we need to do is tell Django, Hey, I want you to connect to this HTML file for this view.

80
00:05:27,890 --> 00:05:32,600
So this is going to be inside the string first underscore app.

81
00:05:33,500 --> 00:05:35,690
Forward slash example.

82
00:05:36,440 --> 00:05:37,970
Dot h html.

83
00:05:38,480 --> 00:05:40,610
And then we are going to save this.

84
00:05:41,090 --> 00:05:46,010
We still have to actually tell Django where to connect and find this template files.

85
00:05:46,280 --> 00:05:50,690
But before we do that, actually, I want to show you the error that happens when you do not connect

86
00:05:50,690 --> 00:05:53,540
them that way, you can understand that error message.

87
00:05:53,780 --> 00:06:00,140
So what we've done so far is I've created a view, and again, this connects to domain dot com for slash

88
00:06:00,140 --> 00:06:03,260
first app, and our case is just one two seven zero zero one.

89
00:06:04,100 --> 00:06:06,050
That's asking, Hey, give me the simple view.

90
00:06:06,560 --> 00:06:07,910
Simple View says no problem.

91
00:06:07,910 --> 00:06:15,890
I take in that request, and then I render first app example that each HTML and that lives here under

92
00:06:15,890 --> 00:06:17,150
my site templates.

93
00:06:17,270 --> 00:06:19,250
First app example the HTML.

94
00:06:19,550 --> 00:06:23,480
So you can see where that line of first app example is.

95
00:06:23,480 --> 00:06:24,470
HTML it's coming from.

96
00:06:24,470 --> 00:06:26,600
It's here, first app example each HTML.

97
00:06:27,020 --> 00:06:31,420
The question is how is Django actually going to know to look for it inside this particular templates

98
00:06:31,430 --> 00:06:31,820
folder?

99
00:06:32,150 --> 00:06:37,580
Well, we haven't actually told it yet, so we're going to do is see what happens when we actually run

100
00:06:37,580 --> 00:06:38,930
this by saying Python.

101
00:06:40,110 --> 00:06:42,510
Managed up by Ron Server.

102
00:06:43,450 --> 00:06:44,130
Hit enter.

103
00:06:44,670 --> 00:06:49,690
So now it's running, I'm going to bring in the actual website.

104
00:06:50,010 --> 00:06:56,280
One two seven zero zero one 8000 and then we're going to ask for first underscore app.

105
00:06:56,880 --> 00:06:58,170
And you should get an error.

106
00:06:58,380 --> 00:07:04,530
It should say, Hey, this template doesn't exist, but as you develop in Django, you'll often get

107
00:07:04,530 --> 00:07:08,220
this sort of weird pass error where it says, Hey, sorry, I can't find the template.

108
00:07:08,730 --> 00:07:12,060
And the best way to debug this is a scroll down here.

109
00:07:12,810 --> 00:07:18,660
There's this super useful template loader postmortem, which is essentially saying, Hey, Django tried

110
00:07:18,660 --> 00:07:24,270
loading a bunch of templates and it's search the bunch of folders, and I could never find this actual

111
00:07:24,750 --> 00:07:27,480
first app example each HTML that you told me to find.

112
00:07:27,870 --> 00:07:32,550
And if you scroll down here, you'll notice it actually reports back where it's searching, which is

113
00:07:32,550 --> 00:07:35,760
super useful when you're debugging where your templates are.

114
00:07:36,570 --> 00:07:42,870
So you can see it tries loading this first one and this is a default location, or it's trying to load

115
00:07:42,870 --> 00:07:48,470
stuff, which is the admin templates that come actually built in to Django.

116
00:07:48,480 --> 00:07:51,120
So it has built in templates for administration.

117
00:07:51,510 --> 00:07:55,380
And you'll notice it also has built in templates for authorization tools.

118
00:07:55,380 --> 00:07:58,060
So it said, Hey, I tried searching for this.

119
00:07:58,080 --> 00:08:03,060
An example HTML first app inside admin for slash or slash templates.

120
00:08:03,060 --> 00:08:03,750
I kind of find it.

121
00:08:04,020 --> 00:08:06,360
I also tried off templates and I couldn't find it.

122
00:08:06,810 --> 00:08:12,360
So what we need to do is actually add on this directory of templates that we just made to the settings

123
00:08:12,360 --> 00:08:14,040
that py file again later on.

124
00:08:14,310 --> 00:08:17,100
We won't do it at a project level, do it at an application level.

125
00:08:17,340 --> 00:08:24,300
But just keep things simple right now, I'm going to open up the settings that py file underneath my

126
00:08:24,300 --> 00:08:24,750
site.

127
00:08:26,060 --> 00:08:30,500
So I'm open up settings about pie, and there's two things I want you to be aware of here.

128
00:08:31,040 --> 00:08:38,240
The first one is this list of installed apps you'll notice those admin and authorization apps are already

129
00:08:38,240 --> 00:08:40,820
installed by default in settings that pie.

130
00:08:41,240 --> 00:08:47,840
Later on, we'll need to add our own applications to this list of installed apps, for example, right

131
00:08:47,840 --> 00:08:48,210
now.

132
00:08:48,230 --> 00:08:55,760
First app is technically not an installed app, so we'll have to use migrations and install that application

133
00:08:55,760 --> 00:09:00,710
later on in the next section of the course in order to set everything nicely so that templates can live

134
00:09:00,710 --> 00:09:01,910
within their own applications.

135
00:09:02,330 --> 00:09:07,310
But right now, we're going to kind of skip that step to go in a simpler direction, which is a project

136
00:09:07,310 --> 00:09:08,420
level template folder.

137
00:09:08,840 --> 00:09:12,980
And if you scroll down here, eventually, you should see templates.

138
00:09:13,400 --> 00:09:17,180
So templates is this list that contains this dictionary?

139
00:09:17,630 --> 00:09:22,190
Scroll down a little to further, which contains this dictionary, which says, Hey, I'm going to look

140
00:09:22,190 --> 00:09:25,770
for things in my default back end Django templates.

141
00:09:25,790 --> 00:09:29,450
But if you want, you can tell me any other directories.

142
00:09:29,630 --> 00:09:37,700
You want Django to search for templates, and you can also say our application directories OK to look

143
00:09:37,700 --> 00:09:39,080
for template folders.

144
00:09:39,470 --> 00:09:41,690
So this is eventually we're going to be using.

145
00:09:41,690 --> 00:09:46,460
We're just going to say, hey, automatically do this for the applications, but for right now, we're

146
00:09:46,460 --> 00:09:46,970
going to do it.

147
00:09:47,330 --> 00:09:52,760
And the main reason I want to do it this way is that you feel very comfortable just going into the settings,

148
00:09:52,760 --> 00:09:56,000
not py file and you understand what's actually going on here.

149
00:09:56,030 --> 00:10:00,260
So put simply, the settings that PY file contains this templates variable.

150
00:10:00,530 --> 00:10:05,690
And then of that, you can tell it, Hey, I want you to look at these locations for directories, for

151
00:10:05,690 --> 00:10:06,200
templates.

152
00:10:06,470 --> 00:10:09,410
And so what we're going to do here is at the very top.

153
00:10:10,450 --> 00:10:17,050
You notice they automatically builds out what's known as a base directory for you, and we can actually

154
00:10:17,050 --> 00:10:19,840
use that in order to join it with our Templates folder.

155
00:10:20,200 --> 00:10:22,750
But to do that, we have to say import.

156
00:10:23,660 --> 00:10:30,680
OS x OS or operating system is this library that's built in Python that allows us to quickly join together

157
00:10:30,680 --> 00:10:35,540
paths automatically so that we I don't need to worry if your folder names are different than my folder

158
00:10:35,540 --> 00:10:35,840
names.

159
00:10:36,050 --> 00:10:39,350
When I finally deploy and export this project, they should just work.

160
00:10:39,710 --> 00:10:44,090
So Base Directory here, that's building out your base directory where everything lives.

161
00:10:44,360 --> 00:10:50,030
What I want to do is join the base directory with my current template directory.

162
00:10:50,270 --> 00:10:51,320
So I come down here.

163
00:10:52,560 --> 00:10:55,350
And I'm going to do the following inside of directories.

164
00:10:55,380 --> 00:10:56,640
I'm going to say o-C.

165
00:10:57,840 --> 00:10:59,860
That path that join.

166
00:11:00,570 --> 00:11:04,620
And I'm going to join out my base directory with.

167
00:11:05,510 --> 00:11:10,940
The templates folder I just created, so I say templates forward slash.

168
00:11:11,180 --> 00:11:16,160
So what's actually happening here is you're telling Jane go, hey, there's some other directories I

169
00:11:16,160 --> 00:11:22,090
want you to check when looking for templates, and it's going to be my base directory, forward slash

170
00:11:22,100 --> 00:11:23,590
templates, forward slash.

171
00:11:23,600 --> 00:11:27,050
And then you can hopefully see the example here that would then be forward slash.

172
00:11:27,050 --> 00:11:32,600
First up, for example, each HTML, which is exactly where the slips my site templates, for example,

173
00:11:32,600 --> 00:11:39,620
each HTML, you technically have the capability to have it point to anywhere on the server or on the

174
00:11:39,620 --> 00:11:43,640
computer of Hey, you should look at this folder as well for templates.

175
00:11:44,030 --> 00:11:48,110
There's going to be some issues revolving around how you should actually structure these and named these,

176
00:11:48,380 --> 00:11:50,240
but we'll talk about that in the next section.

177
00:11:51,050 --> 00:11:55,550
For right now, what I want you to feel comfortable with is the idea that you understand inside the

178
00:11:55,550 --> 00:11:57,970
settings that PDF file that you're telling Django.

179
00:11:57,980 --> 00:12:02,810
Here are the directories that I want you to check as well, and you can see you could add in many more

180
00:12:02,810 --> 00:12:03,890
directories to check.

181
00:12:04,340 --> 00:12:09,710
And we're going to do is come back here where it says, Hey, I can find that template refresh, and

182
00:12:09,710 --> 00:12:11,240
now you can see example.

183
00:12:11,240 --> 00:12:13,310
Each HTML has been connected.

184
00:12:13,820 --> 00:12:19,700
OK, so we've been able to successfully connect an HTML template file to a view.

185
00:12:20,030 --> 00:12:23,650
We haven't done anything by passing information to that template yet.

186
00:12:23,660 --> 00:12:25,130
That's what the next section is for.

187
00:12:25,400 --> 00:12:27,920
But I want to quickly review everything that we just did.

188
00:12:28,760 --> 00:12:32,700
So inside are URLs that py file at the application level.

189
00:12:32,720 --> 00:12:38,360
I said, Hey, go ahead and set up this simple view that's going to show up at domain dot com forward

190
00:12:38,360 --> 00:12:41,840
slash first app then because simple view exists there.

191
00:12:42,200 --> 00:12:42,560
I can't.

192
00:12:42,560 --> 00:12:47,180
The views that I created this simple view function it takes in a request and then we learned about a

193
00:12:47,180 --> 00:12:51,830
new function, which is this very convenient rendering function that takes in the request.

194
00:12:52,130 --> 00:12:56,960
And then where that h html actually lives, where is that template later on will actually be able to

195
00:12:56,960 --> 00:12:59,720
present context to this HTML file?

196
00:13:00,170 --> 00:13:03,980
Now, jingo, by default, didn't actually know where this HTML file was.

197
00:13:04,370 --> 00:13:11,150
So we came to settings, and by using a little bit of python, we said Import OS and then take the base

198
00:13:11,150 --> 00:13:15,380
directory path, go ahead and join it with the templates path.

199
00:13:15,530 --> 00:13:20,900
And that's going to essentially tell you, Hey, this is the long string for the directory location

200
00:13:21,110 --> 00:13:22,790
that I want you to check for templates.

201
00:13:23,390 --> 00:13:23,780
OK.

202
00:13:24,260 --> 00:13:28,310
So that's it on the very basics of connecting to a template again.

203
00:13:28,340 --> 00:13:30,470
Later on, we're going to do this in a different fashion.

204
00:13:30,770 --> 00:13:35,120
But in order to do that, we'll have to learn about how to install applications, as well as how to

205
00:13:35,120 --> 00:13:37,910
do migrations and then set directories up differently.

206
00:13:38,210 --> 00:13:42,860
But hopefully you get an idea of what is now possible, which we're going to explore in the next section.

207
00:13:43,160 --> 00:13:43,790
OK, thanks.

208
00:13:44,030 --> 00:13:45,290
And I'll see it at the next lecture.

