← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 5 of 10

Template Filters

Filters are like little machines you pipe a value through with a | symbol to clean it up or change how it looks before printing.

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.

markup
<!-- 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.

markup
<!-- 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.

markup
<!-- 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.

markup
<!-- 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. #}
Common Mistakes
  1. Forgetting the pipe symbol | between the variable and filter name, e.g. writing {{ name upper }} instead of {{ name|upper }}.
  2. Chaining filters in the wrong order and expecting the same result -- filter order matters since each one transforms the previous output.
  3. Trying to pass a filter argument without a colon, e.g. {{ value truncatewords 10 }} instead of {{ value|truncatewords:10 }}.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.