Django Template Filters

Django Template Filters

As we saw in the interpolation tutorial, we are able to output dynamic data in Django Templates using variables the are enclosed in the special opening {{ and closing }} curly brace characters. Django also offers a way to perform various types of data formatting right in the template by use of filters. Filters are like simple built-in functions that you can call using a special pipe | syntax. Filters can also be used inside of expressions to perform simple calculations within the template.


DTL Filter Syntax

To call a filter, you simply add a pipe | directly after the variable name, and then the name of the filter to run.

Without a filter

{{ timeframe }}

With a filter

{{ timeframe|capfirst }}

Filters In Action

The first filter we’ll look at is the capfirst filter. It capitalizes the first character of the variable it is called on. It is highlighted in the template we have been working on here.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ timeframe|capfirst }} goal</h1>
    <h2>{{ goal }}</h2>
</body>
</html>

django capfirst filter

Next up, we can try out the title filter. This filter converts a string into titlecase by making words start with an uppercase character while leaving the other characters in lowercase.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ timeframe|capfirst }} goal</h1>
    <h2>{{ goal|title }}</h2>
</body>
</html>

django dtl title filter

The lower filter does just what you would expect. It converts a string into all lowercase. We can test it on both of our variables here.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ timeframe|lower }} goal</h1>
    <h2>{{ goal|lower }}</h2>
</body>
</html>

dtl lower filter

Filters With An Argument

Filters in Django templates may also accept an argument. One such filter is the add filter. This filter adds the passed argument to the value.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ timeframe|add:' Timeframe' }} goal</h1>
    <h2>{{ goal }}</h2>
</body>
</html>

django template language add filter

Stacking Filters

Filters can be stacked, or chained, together. Let’s see that in action here.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ timeframe|capfirst|add:' Timeframe' }} goal</h1>
    <h2>{{ goal }}</h2>
</body>
</html>

All of the available filters are listed here: add, addslashes, capfirst, center, cut, date, default, default_if_none, dictsort, dictsortreversed, divisibleby, escape, escapejs, filesizeformat, first, floatformat, force_escape, get_digit, iriencode, join, json_script, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phone2numeric, pluralize, pprint, random, rjust, safe, safeseq, slice, slugify, stringformat, striptags, time, timesince, timeuntil, title, truncatechars, truncatechars_html, truncatewords, truncatewords_html, unordered_list, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, and yesno. Learn how each of them work at the filter reference.

In summary, Filters are like simple adjustments or transformations that get applied to values that are rendered in a template with interpolation. They make it possible to remove any formatting type logic from views.py, and keep that type of processing directly in a template.

Learn More About Django Template Filters