Locked lesson.
About this lesson
Filters allow us to work with our variables within a Django project. In this lesson, we'll learn how filters work and how to use them.
Exercise files
Download this lesson’s related exercise files.
20 - filters.docx57.2 KB Filters - Solution.docx
57.6 KB
Quick reference
Filters
Filters allow us to do transformative things to our variables using the Django Template Language.
When to use
Use them whenever you want to programatically transform a variable on a webpage.
Instructions
To find a list of the main filters, head to:
https://docs.djangoproject.com/en/3.0/ref/templates/language/#filters
as well as:
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#ref-templates-builtins-filters
Hints & tips
- https://docs.djangoproject.com/en/3.0/ref/templates/language/#filters
- https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#ref-templates-builtins-filters
- 00:04 Okay, in this video, I want to talk about filters.
- 00:06 And filters allow us to do things to our variables on our web page.
- 00:10 So if we go back to the Django docs that we've been looking at,
- 00:14 we can scroll down to Filters.
- 00:16 And the format is just use a pipe next to the variable and
- 00:19 then just call whatever filter you want.
- 00:21 So there's a bunch of filters,
- 00:23 we're going to look at some of the more popular ones in just a second.
- 00:26 But for now, let's just use this lower one.
- 00:30 This will change everything in your variable to lowercase.
- 00:34 So let's head back over to our code and look at our views.py file.
- 00:37 We have these two variables, f_name and l_name, John and
- 00:41 Elder, capital E, capital J.
- 00:44 And we're passing them each into our page through the context dictionary,
- 00:48 where we can reference them as first_name and last_name.
- 00:51 So if we come over to our about page, we have this first_name,
- 00:54 last_name variable listed here.
- 00:56 Well, let's say we we didn't want it to be capital E and elder.
- 01:00 We could go pipe and then lower, right?
- 01:04 So let's go ahead and save this and head back over to our page, and click Reload.
- 01:10 And we're going to look right here where it says John Elder.
- 01:13 Boom, now it says lowercase elder, that's very, very cool.
- 01:16 So, Python is an object oriented language.
- 01:19 And usually with object oriented things, we can do objecty things, too,
- 01:24 for instance variables by slapping a period at the end of it and
- 01:28 then doing the object oriented thing.
- 01:30 So a lot of times you would take your variable and go variable.lower, and
- 01:35 that does the same thing.
- 01:37 Well, this sort of allows us to do kind of the same thing,
- 01:40 only through the Django template language on our actual webpage.
- 01:44 So very, very cool.
- 01:45 So head back over to the Django documention and
- 01:48 look through the filters list.
- 01:50 And if we scroll down here, you can see a couple of things that are interesting.
- 01:54 You can build your own custom filters, right?
- 01:57 We're not going to talk about that in this course, but
- 01:59 if you're interested in that check out this link right here.
- 02:02 A lot of this is more advanced, but you could build your own filters, or
- 02:05 you can look at this built-in filter reference.
- 02:07 So, I'm going to open this.
- 02:09 And this just is a big list of some of the more popular ones.
- 02:14 Capitalize first for instance, let's take a look at that one.
- 02:17 So let's head back over to our document or to our code.
- 02:21 And I'm going to change this to lowercase elder, right?
- 02:25 So save this, now let's say we wanted to programmatically change this to
- 02:30 capitalize the first letter.
- 02:32 We would just type in capfirst, save this, come back to our page and hit Reload.
- 02:38 And boom, now it's capital E and elder.
- 02:40 So you can go through here and you can see there's a whole long list of these things.
- 02:45 We're not going to go through all of them in this video.
- 02:47 I'll pull out a couple more that we can look at.
- 02:49 They're kind of fun.
- 02:50 For instance, let's look at upper.
- 02:52 Roll back our code, instead of capfirst, let's go upper, save this.
- 02:57 And back to our page and hit Reload, and
- 03:00 then you can see every single letter is uppercase.
- 03:03 That's kind of cool.
- 03:05 We can cut out different letters or things.
- 03:09 So let's say we want to get rid of this d in elder.
- 03:12 We can use the cut filter, which is just cut, and then colon, and
- 03:17 then quotation marks, and then whatever you want to cut out.
- 03:21 So let's say we want to cut out the d in elder.
- 03:24 So if we save this, come back here and hit Reload,
- 03:28 E-L-E-R, and you notice it's lowercase again.
- 03:31 We can string these together.
- 03:34 So let's use the capfirst and we want to cut out the D in Elder.
- 03:40 So we want the capital E to begin with, we want the D to be cut out.
- 03:44 So head back over to our code.
- 03:47 And we just do that by putting another pipe,
- 03:50 and then typing in whatever you want.
- 03:54 So we put capfirst, let's save this, come back here and hit Reload.
- 03:59 Boom, John Eler, the D has been cut and first letter has been capitalized.
- 04:05 We can do a word count.
- 04:07 How many words are in our variable?
- 04:09 Well, we know just looking at it that there's only one word, right?
- 04:13 So let's get rid of all of this, let's go wordcount.
- 04:16 So we save this, come back here and hit Reload, boom, 1.
- 04:20 There's only one word.
- 04:21 We can do length, length is kind of a fun one.
- 04:24 Let's go length, how long, how many characters is our variable?
- 04:30 Back here and hit Reload, boom, 5, why is that?
- 04:33 Because our variable is elder and that has 1, 2, 3, 4, 5 characters.
- 04:40 Very cool and really easy to do.
- 04:41 So if you're interested in these and you want to learn all of them in great detail,
- 04:46 check out this webpage.
- 04:47 It`s just in our Django template page that we've been working on.
- 04:51 Click on Filters, scroll down to the bottom of the Filter section.
- 04:55 And then click this built in filter reference.
- 04:57 And I'll put a link to this in the reference notes to this video.
- 05:00 So that's all for this video.
- 05:01 In the next video, we'll look at tags.
Lesson notes are only available for subscribers.