Create a Template
In this lesson, you’re going to create a template for your Django app so you won’t have to paste all your HTML directly into your views. Django has already provided you with the import statement you’re going to need for this:
from django.shortcuts import render
Now that you have
render(), use it in your function with the template name
# Create your views here. def project_list(request): return render(request, 'projects/index.html')
00:12 This is what we’ve been doing so far. I took away this whole bunch of HTML that we had before and just put back our little message. But what we’ve been doing is essentially pasting HTML directly into our view.
Now, we want to avoid this and instead, we want to render a template. So, Django gives us already in the
views— because this is the common way of doing it—it already gives us this
import statement that we’re going to use.
We’re going to need a
template_name and then there’s some other things that default to
None—so we’re just not going to worry about those for now—but we’re going to need to pass a
request and the
So I’m just going to pass this again to
render(), as requested, and then we’re going to also pass in a
template_name. I’m going to name this template, let’s say… We’re inside of our
projects app, so I’m gonna say
'projects' and then
01:36 Okay, so we don’t need this anymore, but instead, now we’re rendering a template. What’s our server doing? Something changed, reloading, no complaints. Let’s take a look into the browser. What if I reload this?
We’re going to have to do something. Where is it looking? It’s looking at
projects/index.html. Okay, cool. So, this gives us already some kind of hint on how to do this, but the main thing is that we’re going to know Django understands we’re pointing to a template. It can’t find this template.
Then, inside of here, another new folder that we give the same name as our app. In this case, it’s going to be
projects. You might wonder, “What’s this weird double folder structure?” and I’m going to talk about this in just a moment.
that’s what we’re going to say here, and take a look what our server is saying. It’s doing fine, a
GET request, no complaints here. So we head back over to our browser and reload the page, and we’re still getting a
Let’s figure out why is that the case. And the reason for it is that we have not registered this
templates/ folder. So, sometimes Django does this by default, that it finds all the
templates/ folders inside of all the projects, but if you’ve set it up like this and you keep getting the
TemplateDoesNotExist error, as we do right here, we’re going to have to go forward and register it.
This is our second excursion into the
settings.py file. Similar to how we have
INSTALLED_APPS, we can zoom around and then we find a setting that’s called
TEMPLATES. Inside of
TEMPLATES, there is a key called
'DIRS' (directories), and this is where we want to register our new
For making this easier, I’m going to utilize a variable that we have in here called
BASE_DIR (base directory). It’s defined up there and it just tells us the root of our Django project, so I can utilize that to from there navigate to a different path.
So now, Django can find our template because we told it where to look. We just told it to look relatively from our base directory inside of our
projects app, and then inside of the
templates directory. Cool.
So, if you set up everything as you expect it and it’s not finding it, head over to your management app inside of the
settings file, find the
'DIRS' and then just add—you can just copy-paste this, and then just change whatever is the name of your app. It should be able to find it afterwards.
All right! So, now we could go in here and add whatever HTML we want, and we have a nicer separation between the views, where we’re now simply just pointing to this template, an actual HTML file Django template where we can write tons and tons of HTML, as much as we want to, as well as some Django templating language, which we’re going to look at later on. Before we move on, though, in the next video, we’re going to take a quick look about why do we want to have this weird double structure? We’re inside of
projects/, we make a
templates/ folder, we make another
projects/ folder, and only then we place our template in there.
Become a Member to join the conversation.