How to Use Template Filters in Django



Python


In this article, we show how to use template filters in Django.

So template are filters that we can apply to things such as variables, so that we can do something to those variables.

For example, there's a lowercase filter in Django. So we make the string filtered to all lowercase letters.

There's an uppercase filter in Django. So we make the string filtered to all uppercase letters.

There are filters to format strings.

There are filters to get the length of strings.

Django has all types of filters that you can apply to them.

In this code, we'll show a full example of making a string all uppercase and all lowercase.

We'll have 2 strings. Both will be capitalized, meaning they will both have their first character capitalized, while all other characters are lowercase.

We'll then convert one string to all lowercase and the other string to all uppercase.

This will give you a general idea of how template filters work.

So the general format to convert a string to all lowercase is, {{ value|lower }}.

The general format to convert a string to all uppercase is, {{ value|upper }}

So let's build very simple code in the views.py file, so that you can see how this code is implemented.


views.py File

So, in this code, we create 2 variables that hold strings that are capitalized.



So, first, we must import render from django.shortcuts, so that we can render in the template file that we are working with.

We then create a function called index which takes in a parameter request (which is always required).

We then create a 2 strings, statement1 and statement 2.

statement1 will be converted to all lowercase.

statement2 will be converted to all uppercase.

We then create our context dictionary. The context dictionary is the dictionary in which we pass all variables into that we want passed into the template file. In this example, we pass in variables, statement1 and statement2. These are the only 2 variables we created.

The statement, return render(request, 'Articles/statements.html', context), renders the template file, statements.html, passing in the variables through the context dictionary.

So, we've now created the views.py file and passed in the variables to the template file.

We now move to the template file, statements.html.


Template File- statements.html

So, now, we go over the code in the template file, which is statements.html, and the code is very simple.

The code is shown below.



So, we create header tags and in between the first header tag, we put in the code, {{ statement1|lower }}

This makes the statement1 variable all lowercase. It's very simple.

The next line is, {{ statement2|upper }}

This makes the statement2 variable all uppercase.

You can see how easy filters are to apply.

Running the above code gives the following output.

Django template filters- lowercase and uppercase

So you can see how the filters worked and the first statement is all lowercase and the second statement is all uppercase.

To see a thorough list of Django filters, see the official documentation on Django's website at the following link: Django's template filters


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...