Built-in Tags and Filters7:44 with Lacey Williams Henschel
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.
[SOUND] Before we dive into some template tag DIY, 0:04 let's take a look at a couple more really handy built-in tags. 0:08 Just like Python has an if statement, Django also gives us a way to 0:12 handle conditional logic and templates, the it tag. 0:16 Let's take a look. 0:21 Open up course_list.html. 0:23 So we're supposed to have a description for every course, but what if we don't? 0:26 What if that's a recent addition to our models and 0:31 maybe some older courses don't have that field filled in? 0:34 If we just display the description unconditionally, 0:38 we run the risk of looking a little bit silly if we have a section for 0:41 description but nothing to go with it. 0:44 This is where we can use the if tag. 0:47 Something to know is that the if tag is pretty smart. 0:50 You can just do something like check if a field exists such as course description 0:53 and if it does, display it. 0:59 And then you just close your if tag. 1:02 And clean up your formatting. 1:06 So now, we'll only display the course description if it has any data in it. 1:09 So let's go ahead and start our server and see what that looks like. 1:15 So python manage.py runserver on our ports. 1:19 So right now, all of our courses have descriptions which is wonderful, 1:30 we know that all of our data is intact. 1:34 But that's not all the if tag can do, it can check how long a description 1:36 is based on its word count and cut it off if it's too long for us. 1:41 So let's use the word count filter to only display our full description 1:45 if the word count is less than or equal to 5 words. 1:50 If it's longer than that, so five words or over then we'll do something else. 1:55 In this case, we'll display the course description but we'll use 2:01 the truncate words filter to cut off everything over the first five words. 2:06 There we go. 2:15 Just needed to make one little correction there. 2:16 If we refresh our page, we'll see that under Python collections and 2:19 Python testing, we're showing the first five words of the course description, but 2:23 the rest of the course description has been removed and 2:28 replaced with an ellipsis which is this three little dots. 2:31 We should also add a link to the course details page for 2:35 when we truncate the description so people can get to the full description easily. 2:38 Let's add that now. 2:43 So right under this truncate words, we can open an anchor tag, 2:45 and this is a great place to use our URL tag too. 2:48 So. 2:54 Url 'courses:detail' primary key=course.primary key, 2:56 close our URL tag, close our anchor tag and 3:02 we can just make this say read more. 3:06 That's pretty standard. 3:10 Now, we can see that under Python Collections and 3:13 Python Testing we have the link to Read more about this particular course. 3:15 We can click it, check that it works and it does, and we're good to go. 3:20 We also have a date for each course that shows when the course was created. 3:25 But what if we don't like the format that it's in? 3:29 Django has a filter for that too. 3:32 If you give Django a date, you can apply the date filter and 3:34 pass in arguments that specify the format. 3:38 For example, a capital Y is a four digit year and 3:41 a capital F is the month spelled out. 3:45 I will link to the full documentation on the date filter in the teacher's notes. 3:48 So let's try this a couple of different ways if we wanted to say August 3:52 21st 2015 all spelled out, we would do that this way. 3:57 After our end if tag here, 4:02 we can add a new div that says Created on and 4:05 then course.created_at|date. 4:10 And then this is where we pass in those arguments that specify the format. 4:16 So in the case of August 21st 2015, all spelled out. 4:19 We use a capital F for the month, a lowercase j for the day and 4:24 then an uppercase Y or capital Y for the four digit year. 4:29 Let's refresh our page and 4:35 we can see that this courses were all created on June 1st 2015. 4:37 If we wanted to change the format of the date we don't have to do any other 4:42 processing we just need to replace this f, j, y with a different format. 4:47 So let's say, we wanted to do the number of the month, slash the date, 4:53 slash the two digit year. 4:57 We would do lower case m/d/y. 4:59 Save > Refresh, and we can see that that works. 5:06 We now have 06, 01, 15. 5:12 This is not something you need to memorize. 5:14 Always refer to the documentation to refresh your memory on 5:17 how to make your dates more flexible. 5:21 It isn't cheating. 5:23 Nobody memorizes this stuff. 5:24 For our final trick in this video, let's head back into views.py 5:26 in our courses app to add an email address to course list view. 5:30 So we'll create a new variable called email. 5:36 And we'll just call it email@example.com. 5:39 We also wanna make sure to pass in this email variable so 5:44 that our template gets it. 5:49 Now, back in course list, we wanna create a footer 5:52 that will allow our users to send us an email if they have any questions. 5:56 So we can just say, Have questions? 6:01 Contact us. 6:04 And then the standard way that you set up an email on the web, 6:06 is just to do a href, make this a mailto so that whenever you click on it, 6:09 it automatically starts your email program and and opens up an email. 6:14 Pass in our email variable and then for 6:19 the display text we'll just use our email variable again. 6:22 We refresh the page, and we see our handy little footer at the bottom. 6:28 Now, our email address showed up and it is technically correct. 6:32 But we had to include this anchor tag, and 6:36 we had to type out the email variable twice. 6:39 That's a little annoying, and 6:41 it's not really in line with Django's don't repeat yourself philosophy. 6:43 That's why Django provides the urlize tag, so 6:47 we can get rid of this whole anchor tag, and 6:51 just use the email variable with the urlize filter. 6:55 And when we refresh our page, we see that we still have a valid email address 7:01 that's a clickable link, ready for us. 7:06 The cool thing about urlize is that it automatically links as a mail 7:09 to link if the text is a valid email address. 7:14 Otherwise it treats the variable as a normal URL link to an external website. 7:18 Pretty smart and it saves us from having to repeat ourselves. 7:23 How DRY are we? 7:27 There are so many amazing and helpful built-in tags and filters in Django. 7:30 We've just touched on a few of them. 7:36 But it's time to move on and start writing our own template tags in the next video. 7:38
You need to sign up for Treehouse in order to download course files.Sign up