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
Filters are handy for displaying data on your web page in a dynamic way, without having to write special functions to control display.
More on Humanize: The Django documentation on the humanize
set of filters.
Now that we have our CSS setup and
0:00
ready to go, let's see about making our
data more dynamic using template filters.
0:02
Remember that a filter is
applied to a variable that you
0:08
pass into your template from your view,
and uses the pipe notation.
0:11
Let's look at some of the filters
that Django includes for us.
0:16
Open up coursedetail.html.
0:21
So let's add a section at the top
0:23
that lists out all of the steps
in this course in paragraph form.
0:27
So, these are the steps in this course,
0:31
c ourse.step_set.all.
0:38
Let's start our server and
see what this looks like.
0:44
So we changed directories into our
learning site, and then run the command,
0:48
python manage.py runserver
on our favorite port.
0:52
Now we can use the eye
to preview our page and
0:59
we'll go into courses and select
Python Basics to be our test course.
1:02
And now we can see
the sentence that we added.
1:07
But, this doesn't render very well,
does it?
1:09
The new information that we added
is appearing in these brackets, and
1:12
it's not very readable.
1:15
We can use a join filter to break
up the items between these brackets
1:18
without having to go into a for
loop or doing any other processing.
1:22
So let's try that.
1:26
Right here after all,
we add the pipe character, and
1:28
then the word join because
we're using the join filter.
1:32
And now we have to pass in the thing
that we want to separate our items by.
1:36
And we do that by using a colon and
then opening some quotation marks.
1:41
Whatever we want to use to separate these
items will go between the quotation marks.
1:46
In this case since we're adding these
items to the end of a sentence,
1:50
I want to separate each step
with a comma and a space.
1:55
Now we close the quotation marks,
save the template, and
1:59
we can refresh our page to
see what this looks like.
2:03
Now this is much better.
2:07
But if we use the length filter, we can
make this sentence even more useful.
2:09
Using the length filter gives you
the number of items in a particular set.
2:14
So let's see how that works.
2:18
So instead of saying these
are the steps we can say
2:20
there are a specific number of steps.
2:24
And we can get at that number
by doing course.step_set.all
2:27
using the pipe character again and
adding the length filter.
2:34
The way the length filter works is this,
2:38
even though we have this whole
expression course.step_set.all
2:42
that uses our whole step set,
we won't see any of the data in that set.
2:47
We won't see the titles that we see
later on in the sentence for example.
2:52
Instead, the length filter just
gives us the length of the set, or
2:56
in other words,
the number of items in the set.
3:01
So if we save this template and reload our
page, we should see a number, and we do.
3:04
There are two steps in this course.
3:10
This works fine, but we should really be
using a built-in method called count.
3:12
The length filter causes a large database
query to be run, so it saves our
3:18
database some trouble if we use count
instead, because it's more efficient.
3:22
So let's make that change.
3:27
We can replace all type
length with just count.
3:29
And whenever we refresh our page
we see that nothing has changed,
3:35
everything is the same.
3:39
We've just made our website more
efficient with this very simple change.
3:40
Now, the parenthesis s parenthesis
that we have in the word steps isn't
3:44
very readable.
3:49
This is where another filter comes in.
3:51
We can use the pluralize filter
to make the word step singular or
3:53
plural based on the number
of steps in that set.
3:58
So let's try this out.
4:02
We just delete this entirely, and
replace it with course.step_set.count and
4:03
then that pipe character and
the word pluralize.
4:10
This one looks a little weird because you
have to type the word step as singular and
4:14
then immediately follow it with
the set you're referring to in
4:20
the pluralize filter.
4:24
But basically, you're telling Django to
make this particular word, step, plural or
4:27
singular based on the number of steps
in this set that you've attached to it.
4:32
Again, the contents of the set won't
render and the number won't render.
4:37
The only thing you'll see is an S
on the end of the word step or
4:43
not, depending on how many
steps are in this course.
4:47
Let's refresh the page, and
you can see what I mean.
4:51
And there you have it.
4:55
There are two steps plural in this course.
4:56
There are also some other template tag
libraries that we can use in our projects.
4:59
Let's test one out in the next video.
5:04
You need to sign up for Treehouse in order to download course files.
Sign up