Using Template Libraries4:22 with Lacey Williams Henschel
Learn how to use filters from external libraries in your project, and also learn about some more built-in tags and filters.
Welcome back. 0:00 What if we wanted to follow Associated Press style for our site? 0:01 The Associated Press, 0:06 a media company, has a style guide that governs how they print numbers. 0:07 For example, for numbers one through nine, they spell out the number but for 0:12 numbers ten and over, they use the numeric. 0:16 Django has a filter for that. 0:20 If we add the AP number filter to our count method, then AP number will 0:22 decide whether to use letters or numbers to represent the length of the set. 0:27 First, we have to load this template library into our project. 0:32 So open up settings.py. 0:36 And in the installed app section, 0:39 you're going to add django.contrib.humanize. 0:43 Now, we have to add load humanize to the top of our coursedetail.html template. 0:49 This just let's Django know that we're planning on using the tags and 0:58 filters that are in this library. 1:01 Finally, we add the filter. 1:04 So down here, right after course.step_set.count, 1:06 we add the pipe character and then we type apnumber. 1:10 Now we save our template, and we can change directories into learning site, 1:15 so that we can start our server and see what happens. 1:21 Now we can see that the number two is spelled out two, 1:33 instead of being represented by the number 2. 1:36 Now over the last video and 1:41 this one, we've made that one sentence a lot more dynamic and useful. 1:43 But let's jump over to step detail dot html to see what else we can try out. 1:48 Sometimes, when you know that you're going to be chaining a lot of filters to 1:54 one particular variable, it's useful to make that variable a little bit shorter, 1:58 so you have to type fewer bites. 2:03 Django let's you do this on the fly with the width tag. 2:06 Since we're going to be adding some extra functionality around the course content, 2:09 in this video and the next ones, let's go ahead and 2:13 make step.content a little shorter. 2:17 So right here we can just open up a with tag, 2:20 and we can choose a new variable name for step.content. 2:23 So I'm gonna call it con, short for content. 2:28 And then whenever we're finished using the variable, we can just say end with. 2:35 And now we can replace step.content with con. 2:41 When we go back to our page, and 2:47 we go to a step detail page, then we can see that nothing has changed. 2:49 We were able to change a variable name in our template without breaking anything. 2:55 While we're inside this width block, 3:00 we can use the shorter variable named con to refer to step.content. 3:03 It doesn’t save us a lot of typing right now, 3:08 but we're going to be adding some extra features to this page 3:11 that have to do with the step content, so this will help us out soon. 3:14 In a later video we're also going to write a custom filter that 3:18 will help us estimate how long it will take someone to finish a course, 3:22 based on how long the description is. 3:26 To get ready for 3:29 that, let's learn about one more built in template filter Word Count. 3:29 Word Count does exactly what it sounds like. 3:35 It gives you the number of words in a particular variable. 3:37 So let's try it out. 3:41 Right here after con pipe line breaks, we can add contents 3:42 and then con pipe word count words. 3:50 Refresh our page, and 3:57 now we can see that there are 26 words in the content of this particular step. 3:59 That was awesome. 4:05 We've made some information about our courses and steps a little more dynamic. 4:07 In the next video, we'll learn how to write our own custom template tags and 4:12 filters for when the ones that Django provides us, don't quite get the job done. 4:16
You need to sign up for Treehouse in order to download course files.Sign up