Template Filters
In this page:
Applying a Basic Filter
The pipe character | applies a filter to the variable on its left, transforming the output before it's printed.
Example: Applying a Basic Filter
|upper converts the string to uppercase; |length outputs the number of characters in the string.
<!-- context = {'title': 'django templates'} -->
<h1>{{ title|upper }}</h1>
<p>{{ title|length }} characters</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Filters with Arguments
Some filters accept an argument, written after a colon, such as truncating text to a set number of words.
Example: Filters with Arguments
truncatewords:5 cuts the text down to the first 5 words followed by an ellipsis.
<!-- context = {'article': 'Django makes building web applications fast and enjoyable for developers.'} -->
<p>{{ article|truncatewords:5 }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Chaining Multiple Filters
Filters can be chained with multiple pipes; each filter receives the output of the previous one, applied left to right.
Example: Chaining Multiple Filters
|lower converts to lowercase first, then |trim removes the surrounding whitespace from that lowercased result.
<!-- context = {'bio': ' DJANGO DEVELOPER '} -->
<p>{{ bio|lower|trim }}</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Providing Fallback Values with default
The default filter outputs a fallback value when the variable is falsy (empty string, None, 0, or missing).
Example: Providing Fallback Values with default
Since nickname is an empty string (falsy), the filter substitutes friend instead.
<!-- context = {'nickname': ''} -->
<p>Hello, {{ nickname|default:"friend" }}!</p>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting the pipe symbol | between the variable and filter name, e.g. writing {{ name upper }} instead of {{ name|upper }}.
- Chaining filters in the wrong order and expecting the same result -- filter order matters since each one transforms the previous output.
- Trying to pass a filter argument without a colon, e.g. {{ value truncatewords 10 }} instead of {{ value|truncatewords:10 }}.
- Filters transform a variable's output using the pipe syntax: {{ variable|filter }}.
- Some filters take an argument after a colon, like {{ text|truncatewords:30 }}.
- Common built-in filters include upper, lower, date, length, default, and truncatewords.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: