Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Views that return plain text are fine, but eventually we're going to want some fancy HTML.
App-specific templates are best kept in a structure like app_name/templates/app_name
because Django looks in app directories for a directory named templates
and makes those templates automatically available.
{{
and }}
are used to mark a variable you want printed out.
{%
and %}
mark template tags, or special bits of Python that Django's template engine knows how to run. Unlike Jinja2 templates, you can't just run arbitrary Python in a template.
render()
turns a request object, a template, and an optional context dictionary into a generated string. More about render
.
-
0:00
[MUSIC]
-
0:03
[SOUND] >> Templates in Django are a lot like
-
0:06
templates in Flask.
-
0:07
If you're not familiar with the idea of a templating system,
-
0:10
it usually involves a few parts.
-
0:12
Templates are usually HTML that has special tags or commands in it.
-
0:16
These tags let you print out data, create loops and conditionals,
-
0:19
and perform other programming constructs.
-
0:22
Templates are also often inheritable, or extendable.
-
0:25
So you can write small templates that fit into larger ones to save yourself trouble.
-
0:29
In Django, templates can be any language that you want.
-
0:32
HTML, JSON, XML, or something else entirely, they just have to be text.
-
0:37
We're gonna be creating HTML templates though,
-
0:39
because we want to give our users pages to look at in their browsers.
-
0:42
So the first template that I want to make is one for
-
0:45
the course list view that we just made.
-
0:47
You remember looking over here in views.py.
-
0:51
What we have right now is fine, I mean, it lists out the names.
-
0:55
But, it's not really useful.
-
0:57
So, let's fix that.
-
0:58
Now, by default, Django looks for
-
1:00
a templates directory inside of your app directory.
-
1:04
And we don't have one so we need to make one.
-
1:07
So here in courses we're gonna make a new folder, and we're gonna call it templates.
-
1:14
Now, it also expects inside templates that you'll have a folder or
-
1:19
directory with the same name as your app.
-
1:22
So, New Folder, courses.
-
1:27
So we have courses > templates > courses.
-
1:31
Now the reason that we do this is so that we have all of
-
1:34
our app-specific templates inside this namespaced directory, courses.
-
1:39
And then if we need to let people override them,
-
1:43
they just make their own template directory named courses.
-
1:45
Or, if we want to have templates that are for
-
1:48
multiple sections, we could name them different names, whatever.
-
1:52
Anyway though, it's a really nicely done namespaced way of handling templates.
-
1:57
Okay, so now, inside courses > templates > courses, I'm gonna make a new file
-
2:03
that is named course_list.html, because it's the list of courses.
-
2:08
Now of course, it doesn't have to have the same name as our view, it just happens to.
-
2:13
And I kind of like for them to have the same name so
-
2:15
that I know this template goes to this view.
-
2:18
And I like my names to be done like this, course_list,
-
2:21
because I know that it's a list of courses.
-
2:24
So kind of two ways for me to go, okay, that's what this thing is for.
-
2:28
Now if we were doing one that was only gonna show a single course,
-
2:30
then we might call it course_detail.
-
2:32
Or maybe it was a form for creating new courses,
-
2:35
maybe it's course_create or course_form, something like that.
-
2:39
But like this, we know that it's a list of courses.
-
2:42
Cool.
-
2:43
Obviously, by default, since we just created a file, our template is blank.
-
2:48
So let's put in just a little bit so we can see the titles and
-
2:51
the descriptions of our courses.
-
2:53
We'll decide here that we're gonna provide a list to our template, and
-
2:57
that list will have all the courses.
-
2:59
Django let's us do for loops in our templates, so let's see by doing that.
-
3:04
So we can do for course in courses, and
-
3:08
then we have to end our for loop with the endfor tag.
-
3:13
So now anything that we have in here will get done once for
-
3:17
every course that's in our courses list.
-
3:21
Template tags are the things that let us use little bits of Python in
-
3:24
our templates.
-
3:25
And they always start and end with a pair of characters,
-
3:29
which is the curly brace and the percent sign.
-
3:33
Django's template engine isn't quite as free-form as the Jinja2 template system
-
3:37
that we've used in the Flask course, but you can still do a lot with it.
-
3:41
And actually in Django 1.8 it's also possible to use Jinja2 for
-
3:45
rendering your templates.
-
3:46
But we're not gonna cover that in this course,
-
3:48
we're gonna stick with Django's template renderer for this course.
-
3:51
Okay, so inside this loop we want to print out the title of the course and
-
3:55
the description of the course.
-
3:57
We use two curly braces to print out a variable.
-
3:59
So let's make an h2, and inside of the h2 we'll print course.title,
-
4:05
and then below that we'll print out course.description.
-
4:09
Now since the fields belong to the model instance as attributes,
-
4:13
we access them with the dot, just like we would if we were looking at an object in,
-
4:17
like our shell.
-
4:19
And then we have to end our for loop, which we've already done.
-
4:20
All right, so let's go make our view use our template.
-
4:27
So this is our template.
-
4:28
For course in courses, print out the title, print out the description.
-
4:31
Done, all right, views.py.
-
4:33
What we're gonna do is we're gonna get rid of most of this work.
-
4:37
We don't need this output, and we don't need this return.
-
4:40
So let's get rid of those.
-
4:42
And we're gonna return a new thing, we're gonna return the render function.
-
4:46
And look at that, we've already got it imported.
-
4:48
And so this takes three arguments that we need to give it right now.
-
4:53
So first of all it takes the request object,
-
4:55
this thing that comes in right here.
-
4:58
So it takes that, and then it takes the template to render.
-
5:02
Now, we're going to say 'courses/course_list.html'.
-
5:07
And then, optionally, it takes a dictionary.
-
5:12
We call this dictionary a context dictionary,
-
5:15
because it's the context with which the template will be rendered.
-
5:20
And we're going to provide it a key named courses, and
-
5:23
it's going to be our thing we picked up earlier named courses.
-
5:29
This right here, it's all of our courses.
-
5:31
So, we've got all this stuff.
-
5:34
There are some other arguments that render can take,
-
5:37
I'll link to more information in the teacher's notes.
-
5:39
But for here though we're just handling this fairly simple, straightforward use.
-
5:45
So we need to run our server.
-
5:56
If your server is already running, you won't have to do that.
-
5:58
Mine died, so I had to restart it.
-
6:01
And then if I come back over here and I look at courses, check that out.
-
6:07
That looks, well, maybe not great, but it looks okay.
-
6:10
So, this covers, I mean this is creating a template for
-
6:14
an app, that's fairly straightforward.
-
6:16
But what if I wanted to have a layout for
-
6:17
the homepage, the page that we made, let's go look at it here.
-
6:23
Page that we made over here, this hello_world.
-
6:26
The one that we get by just going to forward slash.
-
6:29
What if I want a template for this page?
-
6:30
Its view doesn't live inside of an app.
-
6:33
So we can't just add a template's directory to an app and be done with it.
-
6:36
We could have template for it to our courses app, but that doesn't make a lot
-
6:41
of sense because that page may not come with our courses app.
-
6:45
So, how do we handle this?
-
6:46
Well, what we're gonna do is we're going to add a directory.
-
6:49
Let's close this stuff up here.
-
6:51
We're gonna add a directory into our outermost learning site here, and
-
6:55
we're gonna name this directory templates.
-
7:00
And we have to go do one more thing.
-
7:03
So we have to go change our settings.
-
7:05
So let's go look at our settings.
-
7:08
Here's our settings, and we're gonna come down here, and we've got templates.
-
7:15
And then we've got this list here called DIRS.
-
7:18
So this templates thing here is a list, and each item on the list is a dictionary,
-
7:25
and each of those dictionaries describes one way of rendering templates.
-
7:32
So for instance, this one is the BACKEND that renders Django templates,
-
7:35
this is just the Django templates renderer.
-
7:38
This is where we had change to like the Jinja2 template renderer.
-
7:41
And this APP_DIRS directory here is the one that says look for
-
7:45
templates directories inside of apps.
-
7:47
That's what lets us do the templates courses thing, so that's pretty cool.
-
7:51
Options, these are just different things that you can configure per template
-
7:55
renderer.
-
7:56
Some of them you don't want to have debugged, maybe you don't want to have
-
7:58
the request object, maybe you don't want to have messages, whatever.
-
8:02
What we care about though, is this DIRS list.
-
8:05
So this DIRS list lets us specify other
-
8:09
directories that we want included for it to go look for templates in.
-
8:14
So, this actually works from the root of the site.
-
8:17
So this is the root of our site.
-
8:20
So how do we find in here the directory?
-
8:22
Well, we named the directory templates, so let's just add in the string, templates.
-
8:27
And I'll put a comma in there because that's a good habit.
-
8:30
Okay, so now we need to create our template, and our view,
-
8:33
and change our view.
-
8:35
So let's make a new file here, and we'll call it,
-
8:37
say, home.html, since it's the homepage.
-
8:42
And then, inside here I'm gonna put an h1 and it's just gonna say welcome.
-
8:46
And I think that's good enough for now.
-
8:48
So it just says welcome.
-
8:49
I mean, right now, it says, Hello World, we'll say, welcome.
-
8:53
So, now let's go back and edit our views.
-
8:56
So here's our views.
-
8:58
And what we need to do is we need to change this to shortcuts, and render.
-
9:06
And then we're going to return render (request,
-
9:11
and our template is 'home.html').
-
9:15
And we don't have to give a directory on this one because it doesn't live inside
-
9:18
of another directory.
-
9:19
We could have added say, default or basic or whatever, but
-
9:24
this one doesn't live inside anything.
-
9:27
And then we don't have to include the context directory, or
-
9:30
dictionary because there's no context.
-
9:33
Okay, so let's refresh this.
-
9:36
And we get our Welcome, great.
-
9:38
You might want to change the view name from, hello_world, to, home or home_page.
-
9:43
Be sure you change the URL too, since it has the view name in it.
-
9:46
In our next video,
-
9:47
we'll look at the other important part of a templating system, template inheritance.
You need to sign up for Treehouse in order to download course files.
Sign up