Filters and Comments

Let’s learn how to use filters and comments in templates.

Filters

Filters are used to modify variables for display.

Syntax

Below is the syntax of the filter:

{{ variable | filter_name }}

A | (called a pipe) is used to apply a filter. Basically, the syntax above will display the value of the {{variable}} after being filtered through the filter_name filter.

Some filters can also take arguments. A filter argument looks like this: {{ bio|truncatewords:30 }}. This will display the first 30 words of the bio variable.

Django provides about sixty built-in template filters. Below are some of the more commonly used template filters:

Get hands-on with 1400+ tech skills courses.