Built-in Django filters
Django filters are designed to
format template variables. The syntax to apply Django filters is
the vertical bar character |
also known as 'pipe' in
Unix environments (e.g.{{variable|filter}}
). It's
worth mentioning that it's possible to use multiple filters on the
same variable (e.g.{{variable|filter|filter}}
).
I'll classify each built-in Django filter into functional sections so it's easier to identify them. The functional classes I'll use are: Dates, Strings, Lists, Numbers, Dictionaries, Spacing and special characters, Development and Testing & Urls.
Tip You can apply Django filters to entire sections with the {% filter %} tag. If you have a group of variables in the same section and want to apply the same filter to all of them, it's easier to use the {% filter %} tag than to individually declare the filter on each variable. The next section in this chapter on Django built-in tags provides more details on the {% filter %} tag
Dates
date
.- Thedate
filter formats Pythondatetime
objects and only works if the variable is this type of Python object. Thedate
filter uses a string to specify a format. For example, if a variable contains adatetime
object with the date 01/01/2022, the filter statement{{variable|date:"F jS o"}}
outputs January 1st 2022. The string syntax for the date filter is based on the characters described in table 3-3.
The remaining content for Django 4.0 is only available with a subscription.