Welcome back, everyone, to this lecture where we're going to discuss jingo filters.
Filters are built in modifiers in Django templating that allow you to quickly apply a change to a variable
on the template side rather than in your Python script.
So they're often very similar to a Python based method.
For example, let's imagine that we were passing in a first name key in our template and that the output
was Rosalind.
We can actually apply a filter using this horizontal bar and in the name of the filter.
For example, we could uppercase every letter that shows up in the resulting template by using the upper
filter.
And there are a ton of filters available for us.
So we're going to do is explore a few examples of filters, how to use them, and then we'll show you
or see the full list and the documentation.
Let's head over to Visual Studio code.
OK, here I am back our HTML file.
You'll notice that we passing things like first name, last name and even things like an insight, key
insight, a dictionary.
And recall, this is being passed from this variable here, which is a dictionary with some strings
it lists and another dictionary inside of it.
Let's go ahead and explore how to apply and use these filters.
The main way you do them is you simply pass in the vertical bar there and then the name of the filter
itself.
Now you do have to know these names or look them up in the documentation.
Keep in mind, not every single operation has a filter attached to it, but you can later on create
your own custom filters.
We'll talk about that later, but right now, let's use one that's built in, such as upper.
And depending on whether you not you downloaded an extension, you can get this help of which ones are
available for you.
So we know uppers available based off that.
So I'm going to go ahead and save that change.
And now let's refresh our page.
And you can see if I bring it over now.
Rosslyn is completely in uppercase.
Very similar to calling the upper the string based method within Python.
So clearly, there's some heavy inspiration here happening from Python, of which filters to actually
create and choose.
For example, there's also a length filter.
So I can say length.
Go ahead and save that, and then if I refresh the page here, you can see it now, says eight.
So it's actually calculating the length for us how many letters were in that first name?
Sometimes this will be useful.
Sometimes it won't be.
It's really up to you to decide whether or not it's useful to do this and whether or not it's useful
to do this on the HTML side of things or keep all this sort of logic on the view side.
It really depends on the situation.
Both methods are totally OK.
It just depends on what you prefer and the methods that you like to see and where you like to see that.
Maybe it's too much work here to have the front end handling everything, and you'd rather have everything
be in Python on the back end.
Now let's talk about where to actually find these in the Django documentation.
You can search for built in template tags and filters.
And we haven't talked about tags yet, but basically we're looking for this website or this web page
within the Django documentation.
It's built in template tags and filters, and so they have a giant list of all the tags.
We haven't talked about those yet, so you can ignore that.
B, scroll down here on the right hand so you can see the contents.
Scroll through all the tags and eventually you see something that says built in filter reference.
So this is a huge list of all the filters that are available for you.
Some of these filters can actually be quite complex.
There's filters there to actually grab the dates and times that are currently available on your computer
or on whoever which, whichever client is using the browser they can actually grab based off that browser
on their computer, what time it is or what it is, where they're looking at the web page.
So keep that in mind, but click here and built-in reference.
You can see there's a bunch of filters, and what's really nice about the documentation is pretty much
just tells you an example and how it works.
So a lot of these are pretty self-explanatory.
Some of them are maybe not super useful, for example, cap first.
That's very similar to the capitalized method on a string, and it's just going to make this capitalized.
So why would this be useful, for instance?
Well, maybe you're reading something in from a database and it's a bunch of lowercase names, but you
want them to look good for the user and you just want to make sure that it's capitalized.
Maybe some names are not capitalized and some names are.
So you can just have this on the template side of things just to make sure you quickly capitalize just
in case something doesn't have to be capitalized.
So you can go here, you can check out that it actually centers out based on strings.
This could be very useful for adjusting things on your web page, and you kind of get the idea here.
Again, some of these are way more complex like dates.
You can actually end up formatting dates.
So instead of saying, for example, the 31st Jan, you can say the 31st of the full name January.
So you have a lot of flexibility and there are string codes for each of these dates and descriptions
and example outputs.
That's one of the most complex filter.
There is this one to do with dates and formatting there, but this is probably one of the most useful
because you can quickly edit the way a timestamp or date time object shows up in the actual HTML.
So often it's a lot easier just to send a date time object into the template space, and then you start
messing around with it based off what is available for you here for formatting.
OK.
So scroll down, you can see there's a bunch of them you can actually saw a dictionary.
That's probably something you maybe want to do more on the Python side of things, but you can remember
that if you pass in a dictionary, you might be nice just to sort of on the template side, you can
do a lot more complicated things than just what I'm talking about.
You can see here, there's different outputs, et cetera, and that's basically it so that we work with
these as you just think, Hey, is this a useful case?
Do I want to do this in the Python side of things or a template side of things?
Python, maybe you want to do it for every single thing before actually doing another modification on
the object.
So you decided the Python side.
What's nice about doing it on the template side, you can think of it as more of a last minute check.
So the example I like to give is, you know, you're passing in a bunch of first names and last names
from some database somewhere.
But people have manually entered these names, so you don't know if they forgot to capitalize them or
not or if they lowercase them.
So we could do is just say, OK, just make sure they're all capitalized.
I say the apply the filter and then you say cap first.
So you go ahead and add that one to last name and first name.
So you do that as well.
First, save those changes and then.
When you refresh this, you can see now you made sure that Rosalind and Franklin were capitalized and
we come back to view that's actually make sure they're all lowercase.
To see if that is actually taking effect, so I saved that change and then I can refresh my page.
You notice it's still capitalized.
Well, you can also do is stack filters, so maybe something look like this.
Rosa Lind with a capital L. in there and you come back here and now things start looking weird because
you have this capitalized letter.
One way to kind of deal with this issue instead of fixing some complicated object here is you simply
come back to your HTML and you start stacking these.
So you say, OK, lowercase everything first and then stack the cup first on that.
So then you would refresh this and you notice it lower cases, everything, and then it runs capitalization.
So this is probably one of the more useful ways of using things on the template side of things.
It's more just to make sure that what's displayed to the client, it's always going to be in the same
uniform fashion.
So by stacking things with lower and capitalization first, I can really make sure no matter how I receive
the first name, it's always going to show up nicely to the user.
For example, maybe I have something that looks like this, and then maybe I have a all lowercase Franklin
here.
So instead of having to figure this out on the python in the thing, sometimes it's just way easier
to say, OK, just lowercase everything that's coming in and then capitalize it.
So you could do this double a first name and last name, then it doesn't matter how different or how
ill formatted things are on your Python side or on your database side.
You come here, the user refreshes the page and they're none the wiser.
Everything looks nice to them.
So that's really a nice way to just clean things up.
On the template side of things especially, we don't know for sure what data and what context and what
variables are coming in from Python side of things.
OK.
That's it for this lecture on filters.
I'll see you at the next one.