Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
`TemplateView` is by far the simplest, useful view class, making it a matter of a single attribute to render any template you want.
The .get_context_data()
method is one that you'll find yourself overriding quite often as you need to just stick one tiny thing into the context dictionary. Remember to use super()
on this method, though, so you won't miss any context objects from other parts of the view.
The first generic class based view that
most people use is the template view.
0:00
You've probably already guessed, but this
view automatically renders a template.
0:04
The easiest way to use this view is just
to provide it with a template name and
0:08
set up a route, but this gives me a great
opportunity to show you how to override
0:11
this class's built-in methods
to start customizing your views.
0:15
I'll see you in work spaces.
0:19
All right, so template views are pretty
similar to regular old view views,
0:21
like the one you wanna use for
hello world view.
0:26
Now, like I said in the first video though
or like I said in the first video, that
0:28
home view, this one right here, would be a
great one to replace with a template view.
0:32
Now before I can do that though, I need
to import the template view class, so.
0:38
View, TemplateView.
0:44
I'll just import both of those.
0:46
And then, well I should use it.
0:48
So I'm gonna put this one right here,
right below home so
0:50
that you can see both of them together.
0:54
So HomeView, this is a TemplateView, and
0:55
then I use set the template name
cuz TemplateViews render templates.
1:01
And just like the one above it, this is
going to render the home.HTML template.
1:05
So, I would argue that my class
is less code than the function.
1:11
Maybe not though.
1:17
Maybe I'm wrong.
1:18
But anyway,
with equal amounts or less code.
1:19
I'm done.
1:23
I've got the view created.
1:23
Now, I do need to still
create the URL though.
1:26
Before I go change the URL though, I wanna
talk about something just for a second.
1:29
All I've customized in this class is
this template name attribute, that's it.
1:33
As you probably guessed, this attribute
controls what template the view renders.
1:39
I have tons of others that I can set
to like, what template engine do use or
1:43
the content type to return
the view has all the stuff.
1:48
I'll be doing a lot more
customizing of these things and
1:52
their associated methods later on.
1:55
Let's go make that URL.
1:58
So I'm actually not gonna make a URL
though, I'm just gonna change this one.
2:00
So right now, I have this views.home.
2:04
So I'm gonna change those
under views.HomeView,
2:06
that's what I named it, right?
2:09
HomeView, and then I'm gonna call as_view,
just like before.
2:11
On the HelloWorld.
2:18
So you always end up calling this as view
thing, using it really used to doing that.
2:20
Okay, so now, if I go and
I look at the the very base of my site.
2:25
I should see the exact same template
that I saw before, looks the same to me.
2:32
Yeah, same old thing.
2:37
Now, one thing that I wanna point out
even though you probably already knew it,
2:38
I don't have to put the word
view into the class names.
2:42
In fact, I mentioned this last time.
2:45
I don't have to put view in here.
2:47
That's that's not required at all.
2:49
I just like doing that so that I know
it's a view any time I see the name
2:52
because you might see
a name out of context.
2:55
Maybe inside documentation or
a poor request or a whatever.
2:57
If you'd rather just call it home or
home page or whatever, go for it.
3:02
Use names that make sense to you and
the people that you're working with.
3:07
What though if I need to add or change
some context that when I'm into the view?
3:10
Well, welcome to your
first major customization.
3:16
Major maybe an overstatement there.
3:19
Now, since I set the template
name attribute, and
3:21
obvious thing to change would
be the get template name method,
3:24
which is the method that's called for
getting the template name.
3:28
But we don't have time for
the obvious, right?
3:31
You wanna do something more fun.
3:33
So the method that I wanna
override is get context data.
3:35
So let's stub this out real quick.
3:38
So, def get_context_data, and
3:40
it takes two arguments,
it takes (self, **kwargs).
3:43
If the name wasn't a dead giveaway, this
method is used for getting or actually
3:49
even generating the context dictionary
that's used for rendering the template.
3:54
This is used in set of supplying
a dictionary directly.
3:59
This method doesn't do
a whole lot on its own but
4:03
I don't want to erase
the work that it does do.
4:05
So, I'm gonna go ahead and use super to
get back the results of all the versions
4:07
of this method, all the way up the stack.
4:11
This is a good habit.
4:14
You'll wanna get into this one too.
4:14
So, context = super
().get_context_data(**kwargs).
4:15
And now I want to add a key to
the context, I'm going to add up
4:25
made up number for the number of
games that are being played today.
4:30
This was a real site, I'd pull that
number from a database somewhere but
4:35
you've gotta make do with what you have.
4:38
So context, which I can spell,
["games_today"] = 6, okay?
4:40
And then, I'm done.
4:48
And I need to make sure
the dictionary comes back out so
4:50
I'm gonna return context, okay.
4:52
So now though,
I need to update the dictionary.
4:55
So we know, sorry not dictionary,
I need to update the template.
4:59
The template, you know, the template.
5:01
So I know by looking at this that
it's the home template, and since it
5:04
doesn't belong to an app, I'm gonna guess
it's here in this templates folder, and
5:10
what do you now I'm right,
there is home.html.
5:14
So I'm gonna add this paragraph and
write down here below this,
5:19
this is the easiest way bit.
5:22
So I'll do a p, and then I'll say,
5:26
There {{ games_todaylpluralize:"
5:32
is,are"}} {{ games_today}}
5:38
game{{ games_todaylpluralize }} today!.
5:44
Okay so, this is using the pluralize
filter to handle changing is and
5:52
are depending on how many games that
are printing out the number of games,
5:57
and then pluralizing game based on
how many games there are today.
6:03
Now, games today comes from here where
I set it in my context dictionary.
6:08
So, with this updated template and this
updated bit of view, if I refresh this,
6:14
I should get a paragraph telling
me how many games there are today.
6:19
And I do, there it is,
6:24
so there are 6 games today, that's good
because that's the number that I set.
6:24
Well you might not find yourself using
template view itself all the time,
6:30
you'll probably be using views that
integrate a lot of the abilities of
6:33
template view, because well, most of
the time, you'll be rendering a template.
6:36
Okay, I think it's time to move into
view classes that make use of models.
6:41
Take a little break, get a snack, or take
a walk, and then come back for list and
6:44
detail views.
6:48
You need to sign up for Treehouse in order to download course files.
Sign up