Use a few more built-in tags and filters before wrting our own custom template tag.
wordcount - Counts the words (defined by whitespace) in the variable.
truncatewords:X - Ends the variable after X words and appends an ellipsis if any content was cut off.
urlize - Converts HTTP(S) and email addresses into HTML anchor tags with appropriate links.
let's take a look at a couple more really handy built-in tags.
handle conditional logic and templates, the it tag.
So we're supposed to have a description for every course, but what if we don't?
maybe some older courses don't have that field filled in?
description but nothing to go with it.
This is where we can use the if tag.
Something to know is that the if tag is pretty smart.
and if it does, display it.
And then you just close your if tag.
And clean up your formatting.
So now, we'll only display the course description if it has any data in it.
So let's go ahead and start our server and see what that looks like.
So python manage.py runserver on our ports.
we know that all of our data is intact.
is based on its word count and cut it off if it's too long for us.
if the word count is less than or equal to 5 words.
If it's longer than that, so five words or over then we'll do something else.
the truncate words filter to cut off everything over the first five words.
Just needed to make one little correction there.
replaced with an ellipsis which is this three little dots.
when we truncate the description so people can get to the full description easily.
and this is a great place to use our URL tag too.
we can just make this say read more.
Python Testing we have the link to Read more about this particular course.
We can click it, check that it works and it does, and we're good to go.
We also have a date for each course that shows when the course was created.
But what if we don't like the format that it's in?
Django has a filter for that too.
pass in arguments that specify the format.
a capital F is the month spelled out.
I will link to the full documentation on the date filter in the teacher's notes.
21st 2015 all spelled out, we would do that this way.
And then this is where we pass in those arguments that specify the format.
So in the case of August 21st 2015, all spelled out.
then an uppercase Y or capital Y for the four digit year.
we can see that this courses were all created on June 1st 2015.
processing we just need to replace this f, j, y with a different format.
slash the two digit year.
We would do lower case m/d/y.
Save > Refresh, and we can see that that works.
We now have 06, 01, 15.
This is not something you need to memorize.
how to make your dates more flexible.
in our courses app to add an email address to course list view.
So we'll create a new variable called email.
And we'll just call it questions@learningsite.com.
that our template gets it.
that will allow our users to send us an email if they have any questions.
So we can just say, Have questions?
it automatically starts your email program and and opens up an email.
the display text we'll just use our email variable again.
We refresh the page, and we see our handy little footer at the bottom.
Now, our email address showed up and it is technically correct.
we had to type out the email variable twice.
it's not really in line with Django's don't repeat yourself philosophy.
just use the email variable with the urlize filter.
that's a clickable link, ready for us.
to link if the text is a valid email address.
Otherwise it treats the variable as a normal URL link to an external website.
Pretty smart and it saves us from having to repeat ourselves.
There are so many amazing and helpful built-in tags and filters in Django.
We've just touched on a few of them.
But it's time to move on and start writing our own template tags in the next video.