Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Custom Time Estimate Filter3:36 with Lacey Williams Henschel
This filter will help you estimate how much time it will take a person to complete a particular step in a course on your site.
pluralize - A filter that, by default, returns an "s" when attached to a number that's not 1, and nothing when the number is 1. You can provide different options if needed. More information is available in the official documentation.
register.filter("filter_name", filter_function) or
@register.filter("filter_name") - Registers a filter with the given name.
[SOUND] You've made it to the last stage of this course, 0:00 but it's not time to celebrate just yet. 0:06 We still have a few more things to learn before you finally get your shiny new 0:11 Django templates badge. 0:15 In this video, we're going to build our own custom template filter 0:18 that we can use on variables just like built-in filters. 0:22 Remember, a filter is something that you can add to a Django template variable 0:26 with the pipe character, that helps make the data in your template more dynamic. 0:30 In the case of the pluralized filter, 0:35 you learned in another video that it can automatically make a word plural 0:38 based on the value of a numeric variable in the template. 0:42 So let's write a filter that will help us estimate how much 0:46 time a step in a course will take to complete. 0:50 So let's open up the file we've been keeping our custom template tags in, 0:54 course_extras.py. 0:57 This is where we'll also be adding our custom filter. 0:58 Make a new function called, time_estimate, and have it take in a value. 1:02 So, define time_estimate, and I'm going to call my value, word_count. 1:06 In our case, this value will be a number, the number of words in a steps content. 1:14 This filter is not going to be super scientific. 1:18 This is mostly a thought exercise. 1:21 But let's say that the longer a steps content is, 1:24 the more content we think it covers and the more time we think it will take. 1:27 So with that in mind, let's say that for every 20 words in a steps content, 1:32 we think it will take that many minutes to complete the course. 1:37 So let's code that. 1:41 So minutes = round(word_count/20). 1:43 We're using the round function here, which is a built-in Python function, so 1:50 we get a nice even number. 1:54 We don't want to display 5.4567 minutes, do we? 1:56 This is just an estimate. 2:01 So we estimate that for every 20 words in a word count, 2:03 it will take about a minute to complete that part of the step. 2:08 Now, we just need to return the minutes. 2:12 And we should add a docstring so 2:15 that everyone else who looks at our code knows what this filter does. 2:17 So we can say, Estimates the number of minutes it will take 2:21 to complete a step based on the passed-in wordcount. 2:27 Finally, we need to register the filter. 2:36 Registering filters is almost the same as registering tags. 2:39 We use the decorator, and then instead of saying .inclusiontag or .simpletag, 2:43 we just say .filter and pass in the name of our filter. 2:49 Now we can try it out. 2:55 If we pop over to step detail, we can load our 2:57 course_extras so that we have access to our brand new filter. 3:02 But wait a second, we don't have a variable for 3:07 the word count that we've passed-in from our view. 3:10 Let's see what we can do about that in the next video. 3:14 Well, we made our filter but we need to figure out how to use it. 3:19 Are you thinking what I'm thinking? 3:24 What if there's a way we can use the built-in word count filter 3:26 to get the data we need for this filter? 3:30 Let's find out in the next video. 3:33
You need to sign up for Treehouse in order to download course files.Sign up