Welcome back, everyone, to the final topic for the section, which is connecting to templates, which
will lead us into the next section where we have a much deeper dive into templates and syntax for that.
So realistically, we don't want to have to manually type out each HTML code or HDP responses, insider
views that pie file instead.
We would like to separate out all our templates that has all those HTML files into a separate directory
and then have the views be able to communicate between this directory and render the templates.
So it's kind of a two way street because we want to be able to understand where the actual HTML files
are.
And then we also want to be able to insert information into those template files.
Now connecting to a template directory does require us to inform the Django project settings where to
find these templates, and in this simple example, we're actually going to store all the templates
at a project level.
But keep in mind, this is actually kind of an unusual way to do things is just for demonstration purposes
only.
Later on in the next section, the course will actually explore how to store templates on a Django application
level, which is typically the recommended way of doing things that way.
All your templates associated with an application are grouped together inside that application folder,
but for right now, we're going to explore a little bit of how Django Settings actually works.
So in this lecture, we're going to create a new templates directory, we'll do it at the project level
again.
Later on, we'll learn how to do it at an application level.
We'll create an example each HTML file, then we'll connect that each HTML file to a view.
And then we're going to explore how to actually inform the Django project, where to find this template
directory inside of its settings that pie.
So let's head to Visual Studio code and get started.
OK, here I am.
Views the pie that we've already been playing around with, as well as URLs that pie inside our first
underscore app to simplify everything.
I'm going to delete all the views that we have so far, as well as this article's dictionary, and I'm
going to remove those URL patterns.
And let's kind of start from scratch showing you how to connect a URL to a view then to a template.
So what I'm going to do here is simply say path.
I'll set this as empty and then I'm going to say views dot simple underscore view, which means I need
to find a function called simple view inside of used up pie.
I'm going to save that change and recall this is essentially going to our domain dot com forward slash
first app.
What view that we actually want to show here?
That's going to be the simple view function.
So I save that change.
Go back to the user pie and let's actually create that simple view.
Note this needs to match exactly how it was written.
So this is going to take in a request.
And then what we're going to do is we are going to use the render function and we have to return this.
So we're going to say return the render and this is where you import from Django that shortcuts import,
render and render can take in a bunch of arguments, but it's mainly just the HDP request, the template
name and then the context.
We'll talk a lot more about context later on, but context is what is passed back to that actual template
file.
So the request is just a request.
I was passing into simple view and then the actual template name is something we have to decide.
So I'm going to leave that blank for now because we're going to explore that in just a second.
So what I need to do is actually say, Hey, there's some HTML file somewhere inside my directories,
where should I actually point it to?
So we're going to expand our explore here.
This is where you have to be really careful, where you actually create in this ongoing collapse first
app and collapse my site.
And what I'm going to do is in the exact same directory that managed is in.
So the very top my site, I'm going to create a new folder.
And we are going to call it templates has to be this name templates.
And again, this is at a project level kind of unusual to do it this way that an application template
lives all the way at the project level.
But just keep in mind, is kind of the simple way to do it before we actually learn about migrations.
Next, what I'm going to do is inside of templates for a little bit of organization or create a new
folder that will match the name of my application, which is first app and then inside a first app,
I'll create a new file.
Which will be.
Called example thought each HTML.
So note where this is living, it's under my site templates first app example HTML.
It's not underneath this my site with settings, not pie, URLs, views, et cetera.
Instead, it's living all the way at the manage that pie.
So the very top level I create as templates.
First example HTML.
Later on, we'll do things this way.
But right now, this kind of simplest way to actually render a template.
So you should be able to type out doc, and then it will automatically fill in this HTML templates and
let's create an H1 header that just says something like example each HTML.
Connected.
Save that change.
And now we need to do is tell Django, Hey, I want you to connect to this HTML file for this view.
So this is going to be inside the string first underscore app.
Forward slash example.
Dot h html.
And then we are going to save this.
We still have to actually tell Django where to connect and find this template files.
But before we do that, actually, I want to show you the error that happens when you do not connect
them that way, you can understand that error message.
So what we've done so far is I've created a view, and again, this connects to domain dot com for slash
first app, and our case is just one two seven zero zero one.
That's asking, Hey, give me the simple view.
Simple View says no problem.
I take in that request, and then I render first app example that each HTML and that lives here under
my site templates.
First app example the HTML.
So you can see where that line of first app example is.
HTML it's coming from.
It's here, first app example each HTML.
The question is how is Django actually going to know to look for it inside this particular templates
folder?
Well, we haven't actually told it yet, so we're going to do is see what happens when we actually run
this by saying Python.
Managed up by Ron Server.
Hit enter.
So now it's running, I'm going to bring in the actual website.
One two seven zero zero one 8000 and then we're going to ask for first underscore app.
And you should get an error.
It should say, Hey, this template doesn't exist, but as you develop in Django, you'll often get
this sort of weird pass error where it says, Hey, sorry, I can't find the template.
And the best way to debug this is a scroll down here.
There's this super useful template loader postmortem, which is essentially saying, Hey, Django tried
loading a bunch of templates and it's search the bunch of folders, and I could never find this actual
first app example each HTML that you told me to find.
And if you scroll down here, you'll notice it actually reports back where it's searching, which is
super useful when you're debugging where your templates are.
So you can see it tries loading this first one and this is a default location, or it's trying to load
stuff, which is the admin templates that come actually built in to Django.
So it has built in templates for administration.
And you'll notice it also has built in templates for authorization tools.
So it said, Hey, I tried searching for this.
An example HTML first app inside admin for slash or slash templates.
I kind of find it.
I also tried off templates and I couldn't find it.
So what we need to do is actually add on this directory of templates that we just made to the settings
that py file again later on.
We won't do it at a project level, do it at an application level.
But just keep things simple right now, I'm going to open up the settings that py file underneath my
site.
So I'm open up settings about pie, and there's two things I want you to be aware of here.
The first one is this list of installed apps you'll notice those admin and authorization apps are already
installed by default in settings that pie.
Later on, we'll need to add our own applications to this list of installed apps, for example, right
now.
First app is technically not an installed app, so we'll have to use migrations and install that application
later on in the next section of the course in order to set everything nicely so that templates can live
within their own applications.
But right now, we're going to kind of skip that step to go in a simpler direction, which is a project
level template folder.
And if you scroll down here, eventually, you should see templates.
So templates is this list that contains this dictionary?
Scroll down a little to further, which contains this dictionary, which says, Hey, I'm going to look
for things in my default back end Django templates.
But if you want, you can tell me any other directories.
You want Django to search for templates, and you can also say our application directories OK to look
for template folders.
So this is eventually we're going to be using.
We're just going to say, hey, automatically do this for the applications, but for right now, we're
going to do it.
And the main reason I want to do it this way is that you feel very comfortable just going into the settings,
not py file and you understand what's actually going on here.
So put simply, the settings that PY file contains this templates variable.
And then of that, you can tell it, Hey, I want you to look at these locations for directories, for
templates.
And so what we're going to do here is at the very top.
You notice they automatically builds out what's known as a base directory for you, and we can actually
use that in order to join it with our Templates folder.
But to do that, we have to say import.
OS x OS or operating system is this library that's built in Python that allows us to quickly join together
paths automatically so that we I don't need to worry if your folder names are different than my folder
names.
When I finally deploy and export this project, they should just work.
So Base Directory here, that's building out your base directory where everything lives.
What I want to do is join the base directory with my current template directory.
So I come down here.
And I'm going to do the following inside of directories.
I'm going to say o-C.
That path that join.
And I'm going to join out my base directory with.
The templates folder I just created, so I say templates forward slash.
So what's actually happening here is you're telling Jane go, hey, there's some other directories I
want you to check when looking for templates, and it's going to be my base directory, forward slash
templates, forward slash.
And then you can hopefully see the example here that would then be forward slash.
First up, for example, each HTML, which is exactly where the slips my site templates, for example,
each HTML, you technically have the capability to have it point to anywhere on the server or on the
computer of Hey, you should look at this folder as well for templates.
There's going to be some issues revolving around how you should actually structure these and named these,
but we'll talk about that in the next section.
For right now, what I want you to feel comfortable with is the idea that you understand inside the
settings that PDF file that you're telling Django.
Here are the directories that I want you to check as well, and you can see you could add in many more
directories to check.
And we're going to do is come back here where it says, Hey, I can find that template refresh, and
now you can see example.
Each HTML has been connected.
OK, so we've been able to successfully connect an HTML template file to a view.
We haven't done anything by passing information to that template yet.
That's what the next section is for.
But I want to quickly review everything that we just did.
So inside are URLs that py file at the application level.
I said, Hey, go ahead and set up this simple view that's going to show up at domain dot com forward
slash first app then because simple view exists there.
I can't.
The views that I created this simple view function it takes in a request and then we learned about a
new function, which is this very convenient rendering function that takes in the request.
And then where that h html actually lives, where is that template later on will actually be able to
present context to this HTML file?
Now, jingo, by default, didn't actually know where this HTML file was.
So we came to settings, and by using a little bit of python, we said Import OS and then take the base
directory path, go ahead and join it with the templates path.
And that's going to essentially tell you, Hey, this is the long string for the directory location
that I want you to check for templates.
OK.
So that's it on the very basics of connecting to a template again.
Later on, we're going to do this in a different fashion.
But in order to do that, we'll have to learn about how to install applications, as well as how to
do migrations and then set directories up differently.
But hopefully you get an idea of what is now possible, which we're going to explore in the next section.
OK, thanks.
And I'll see it at the next lecture.