filter capitalize
Capitalise the first character and lowercase the rest.
value|capitalize
{{ "hello WORLD"|capitalize }}outputWhat a template can reach without being handed anything: the filters, tests and globals, and the statements that shape a page. Every live example below is rendered by the same engine this site is built from — edit nothing, the output is the real one.
Seventy-nine filters, written after a value with |. Aliases are noted where they stand in for another.
Capitalise the first character and lowercase the rest.
value|capitalize
{{ "hello WORLD"|capitalize }}outputCapitalise only the first character, leaving the rest.
value|capfirst
{{ "hello"|capfirst }}outputCentre the text in a field of the given width.
value|center(width)
width int{{ "hi"|center(10) }}outputRemove every occurrence of a substring.
value|cut(arg)
arg string{{ "a-b-c"|cut("-") }}outputEscape HTML so the text is safe to write. Markup is left as it stands.
value|escape
{{ "<b>"|escape }}outputAn alias of escape.
value|e
{{ "<b>"|e }}outputEscape text so it is safe inside a JavaScript string.
value|escapejs
{{ "a'b<c"|escapejs }}outputEscape even text already marked safe.
value|forceescape
{{ ("<b>"|safe)|forceescape }}outputIndent each line (after the first unless first) by a width.
value|indent(width, first, blank)
width int or stringfirst boolblank bool{{ "a\nb"|indent(2) }}outputTurn newlines into paragraphs and line breaks, returned as safe HTML.
value|linebreaks
{{ "one\n\ntwo"|linebreaks }}outputTurn newlines into line breaks, returned as safe HTML.
value|linebreaksbr
{{ "a\nb"|linebreaksbr }}outputPrefix every line with its one-based number.
value|linenumbers
{{ "a\nb\nc"|linenumbers }}outputLowercase the text.
value|lower
{{ "HELLO"|lower }}outputRead the text as Markdown and write it as HTML, the conversion an md template ends with.
value|markdown(safe)
safe bool{{ "## From a string"|markdown }}outputReplace occurrences of old with new, at most count times.
value|replace(old, new, count)
old stringnew stringcount int{{ "a-b-c"|replace("-", " ") }}outputMark the text safe; it passes through without escaping.
value|safe
{{ ("<b>"|safe) }}outputRemove the whitespace between tags.
value|spaceless
{{ "<a> </a>"|spaceless }}outputCast the value to text.
value|string
{{ 7|string }}outputStrip tags and decode entities, collapsing whitespace.
value|striptags
{{ "<b>hi</b>"|striptags }}outputTitle-case the text: capitalise each word.
value|title
{{ "hello world"|title }}outputStrip surrounding characters (default whitespace).
value|trim(chars)
chars string{{ " hi "|trim }}outputAn ASCII slug: accents dropped, letters and digits kept, spaces and dashes to one dash, lowercase.
value|slugify
{{ "Un éléphant à l'orée!"|slugify }}outputCut to a number of characters, ending with an ellipsis; combining marks do not count.
value|truncatechars(arg)
arg int{{ "Testing, testing"|truncatechars(8) }}outputCut to a length, ending with an ellipsis, keeping whole words unless killwords.
value|truncate(length, killwords, end, leeway)
length intkillwords boolend stringleeway int{{ "a long sentence about the weather"|truncate(20) }}outputUppercase the text.
value|upper
{{ "héllo"|upper }}outputCount the words.
value|wordcount
{{ "the quick brown"|wordcount }}outputWrap the text at a column width.
value|wordwrap(width, break_long_words, wrapstring, break_on_hyphens)
width intbreak_long_words boolwrapstring stringbreak_on_hyphens bool{{ "one two three four five"|wordwrap(10) }}outputEscape quotes and backslashes for a JavaScript string.
value|addslashes
{{ "it's"|addslashes }}outputPercent-encode an IRI for use in a URL.
value|iriencode
{{ "a path/c?d"|iriencode }}outputMap letters to the digits of a telephone keypad.
value|phone2numeric
{{ "0800cafe"|phone2numeric }}outputFill % placeholders with positional or named arguments.
value|format(*args, **named)
args valuesnamed mapping{{ "%s = %s"|format("x", 1) }}outputThe absolute value of a number.
value|abs
{{ -7|abs }}outputAdd a number, or concatenate strings and lists.
value|add(arg)
arg value{{ 10|add(5) }}outputFormat a count of bytes as a human size.
value|filesizeformat(binary)
binary bool{{ 1234567|filesizeformat }}outputParse a number or string as a floating point value.
value|float(default)
default float{{ "3.14"|float }}outputFormat a number to fixed places, Django style (g groups thousands, u ungroups).
value|floatformat(arg)
arg string or int{{ 3.14159|floatformat(2) }}outputReturn the nth digit of an integer, counting from the right; 1 is the units digit.
value|get_digit(arg)
arg int{{ 12345|get_digit(2) }}outputParse a number or string as an integer, in a given base.
value|int(default, base)
default intbase int{{ "42"|int }}outputRound a number: common, ceil or floor, to a number of places.
value|round(precision, method)
precision intmethod string{{ 3.14159|round(2) }}outputScale a value to a width: the value over the maximum, times the width, rounded half to even.
value|widthratio(max_value, max_width)
max_value numbermax_width int{{ 175|widthratio(200) }}outputAdd the items, or a named field of each.
value|sum(attribute, start)
attribute stringstart value{{ items|sum(attribute="n") }}data{"items":[{"n":1},{"n":2},{"n":3}]}outputGroup items into rows of a given size, padding the last with fill_with.
value|batch(linecount, fill_with)
linecount intfill_with value{{ [1,2,3,4,5]|batch(2)|list }}outputSort a mapping into (key, value) pairs, by key, value or with a case-sensitive order.
value|dictsort(case_sensitive, by, reverse)
case_sensitive boolby stringreverse bool{{ {"b":2,"a":1}|dictsort|list }}outputThe first item of a sequence.
value|first
{{ [1,2,3]|first }}outputGroup items that share an attribute, each group read as grouper and list.
value|groupby(attribute, default, case_sensitive)
attribute stringdefault valuecase_sensitive bool{{ users|groupby("kind")|map(attribute="grouper")|join(", ") }}data{"users":[{"kind":"a","name":"x"},{"kind":"b","name":"y"},{"kind":"a","name":"z"}]}outputA mapping's entries as (key, value) pairs.
value|items
{{ {"a":1,"b":2}|items|list }}outputJoin items with a separator, or a named field of each.
value|join(d, attribute)
d stringattribute string{{ ["a","b","c"]|join(", ") }}outputThe last item of a sequence.
value|last
{{ [1,2,3]|last }}outputThe length: characters of a string, items of a list, keys of a mapping.
value|length
{{ [1,2,3]|length }}outputAn alias of length.
value|count
{{ "hello"|count }}outputTurn the value into a list.
value|list
{{ "abc"|list }}outputApply a filter to each item, or pull a named field out of each.
value|map(attribute, default, filter)
attribute stringdefault valuefilter string{{ users|map(attribute="name")|join(", ") }}data{"users":[{"name":"Ada"},{"name":"Grace"}]}outputThe largest item, or the largest by a field.
value|max(attribute, case_sensitive)
attribute stringcase_sensitive bool{{ [3,1,2]|max }}outputThe smallest item, or the smallest by a field.
value|min(attribute, case_sensitive)
attribute stringcase_sensitive bool{{ [3,1,2]|min }}outputA readable, Python-style print of a value.
value|pprint
{{ {"a":[1,2],"b":3}|pprint }}outputOne item, chosen so the same page renders the same way twice.
value|random
{{ [1,2,3]|random }}outputDrop items that pass a test; with no test, drop the untruthy.
value|reject(test, args)
test stringargs values{{ [1,2,3,4]|reject("odd")|list }}outputDrop items whose field fails a test.
value|rejectattr(attribute, test, args)
attribute stringtest stringargs values{{ items|rejectattr("shown")|list }}data{"items":[{"shown":true},{"shown":false}]}outputReverse a list, or the characters of a string.
value|reverse
{{ [1,2,3]|reverse|list }}outputKeep items that pass a test; with no test, keep the truthy.
value|select(test, args)
test stringargs values{{ [1,2,3,4]|select("odd")|list }}outputKeep items whose field passes a test.
value|selectattr(attribute, test, args)
attribute stringtest stringargs values{{ items|selectattr("shown")|list }}data{"items":[{"shown":true},{"shown":false}]}outputSplit into a number of slices, padding the last with fill_with.
value|slice(slices, fill_with)
slices intfill_with value{{ [1,2,3,4,5]|slice(2)|list }}outputSort items, by a field, with a case-sensitive or reversed order.
value|sort(reverse, case_sensitive, attribute)
reverse boolcase_sensitive boolattribute string{{ ["b","a","c"]|sort|list }}outputDrop repeated items, by value or by a field.
value|unique(case_sensitive, attribute)
case_sensitive boolattribute string{{ [1,2,2,3]|unique|list }}outputRead a named attribute of a host object.
value|attr(name)
name string{% set ns = namespace(city="Rome") %}{{ ns|attr("city") }}outputA fallback when the value is undefined, or false under boolean.
value|default(default_value, boolean)
default_value valueboolean bool{{ missing|default("none") }}outputAn alias of default.
value|d(default_value, boolean)
default_value valueboolean bool{{ missing|d("none") }}outputA fallback used only when the value is null.
value|default_if_none(arg)
arg value{{ none|default_if_none("none") }}outputPercent-encode a string, or a mapping or pairs as a query string.
value|urlencode
{{ {"q":"a b"}|urlencode }}outputTurn web addresses and emails into anchor tags.
value|urlize(trim_url_limit, nofollow, target, rel, extra_schemes)
trim_url_limit intnofollow booltarget stringrel stringextra_schemes list{{ "visit http://example.com"|urlize }}outputBuild a string of attributes from a mapping, as safe markup.
value|xmlattr(autospace)
autospace bool{{ {"class":"x","id":5}|xmlattr }}outputSerialise a value to JSON safe inside a script tag.
value|tojson(indent)
indent int{{ {"a":1}|tojson }}outputWrap JSON in a script tag, optionally with an id.
value|json_script(element_id)
element_id string{{ {"a":1}|json_script("data") }}outputCut to a number of words, ending with an ellipsis.
value|truncatewords(arg)
arg int{{ "one two three four"|truncatewords(2) }}outputCut marked-up text to a number of characters, closing the elements the cut leaves open; the result is markup when the input is trusted.
value|truncatechars_html(arg)
arg int{{ ("<p>one <b>two</b> three</p>"|safe)|truncatechars_html(8) }}outputCut marked-up text to a number of words, closing the elements the cut leaves open; the result is markup when the input is trusted.
value|truncatewords_html(arg)
arg int{{ ("<p>one <b>two</b> three</p>"|safe)|truncatewords_html(2) }}outputRender a list of items and nested lists as HTML ul elements.
value|unordered_list
{{ tree|unordered_list }}data{"tree":["first",["child one","child two"],"last"]}outputLeft-justify the text in a field of the given width.
value|ljust(arg)
arg int{{ "hi"|ljust(5) }}outputRight-justify the text in a field of the given width.
value|rjust(arg)
arg int{{ "hi"|rjust(5) }}outputMap true, false and null to words, comma-separated.
value|yesno(arg)
arg string{{ ok|yesno }}data{"ok":true}outputAdd a suffix (or two, comma-separated) based on a count.
value|pluralize(arg)
arg string{{ count|pluralize }}data{"count":3}outputFormat a date with Django's letters; a time letter is refused.
value|date(arg)
arg string{{ d|date("Y-m-d") }}data{"d":"2026-05-04"}outputFormat a time with Django's letters; a date letter is ignored.
value|time(arg)
arg string{{ t|time("H:i") }}data{"t":"2026-05-04T13:45:00"}outputWords for the span from a date to now (a date you supply, templates read no clock).
value|timesince(now, depth)
now datedepth int{{ d|timesince(now) }}data{"d":"2026-01-01","now":"2026-05-04"}outputWords for the span from now to a date.
value|timeuntil(now, depth)
now datedepth int{{ d|timeuntil(now) }}data{"d":"2026-05-04","now":"2026-01-01"}outputThirty-nine tests, written after a value with is. Many comparison names are aliases of the symbol forms.
True when the value is null.
value is none
{{ none is none }}outputTrue when the value is a boolean.
value is boolean
{{ true is boolean }}outputTrue when the value is the boolean true.
value is true
{{ true is true }}outputTrue when the value is the boolean false.
value is false
{{ false is false }}outputTrue when the value is an integer.
value is integer
{{ 3 is integer }}outputTrue when the value is a floating point number.
value is float
{{ 3.0 is float }}outputTrue when the value is a number (integer, float or boolean).
value is number
{{ 3 is number }}outputTrue when the value is text or safe markup.
value is string
{{ "x" is string }}outputTrue when the value is a mapping.
value is mapping
{{ {} is mapping }}outputTrue when the value is a string, list, tuple or mapping.
value is sequence
{{ [1,2] is sequence }}outputTrue when the value can be looped over.
value is iterable
{{ "ab" is iterable }}outputTrue when the value can be called, like a global function.
value is callable
{{ range is callable }}outputTrue when the value is markup already safe to write.
value is escaped
{{ ("<b>"|safe) is escaped }}outputTrue when a number is odd.
value is odd
{{ 7 is odd }}outputTrue when a number is even.
value is even
{{ 4 is even }}outputTrue when a number divides by the argument.
value is divisibleby(n)
n number{{ 9 is divisibleby(3) }}outputTrue when the value equals the other. Also eq and equalto.
value == other
other value{{ 3 == 3 }}outputAn alias of ==.
value is eq other
other value{{ 3 is eq 3 }}outputAn alias of ==.
value is equalto other
other value{{ 3 is equalto 3 }}outputTrue when the value differs from the other. Also ne.
value != other
other value{{ 3 != 4 }}outputAn alias of !=.
value is ne other
other value{{ 3 is ne 4 }}outputTrue when the value is less than the other. Also lt and lessthan.
value < other
other value{{ 3 < 4 }}outputAn alias of <.
value is lt other
other value{{ 3 is lt 4 }}outputAn alias of <.
value is lessthan other
other value{{ 3 is lessthan 4 }}outputTrue when the value is greater than the other. Also gt and greaterthan.
value > other
other value{{ 3 > 2 }}outputAn alias of >.
value is gt other
other value{{ 3 is gt 2 }}outputAn alias of >.
value is greaterthan other
other value{{ 3 is greaterthan 2 }}outputTrue when the value is at most the other. Also le.
value <= other
other value{{ 3 <= 3 }}outputAn alias of <=.
value is le other
other value{{ 3 is le 3 }}outputTrue when the value is at least the other. Also ge.
value >= other
other value{{ 3 >= 3 }}outputAn alias of >=.
value is ge other
other value{{ 3 is ge 3 }}outputTrue when text is all lowercase.
value is lower
{{ "abc" is lower }}outputTrue when text is all uppercase.
value is upper
{{ "ABC" is upper }}outputTrue when the two values are the same object or equal value.
value is sameas other
other value{% set a = [1] %}{{ a is sameas a }}outputTrue when the value is a member of the container.
value is in container
container value{{ "a" in ["a","b"] }}outputTrue when the name resolved to a value.
value is defined
{% set present = 1 %}{{ present is defined }}outputTrue when the name resolved to nothing.
value is undefined
{{ missing is undefined }}outputTrue when a filter of the given name is registered.
value is filter
{{ "upper" is filter }}outputTrue when a test of the given name is registered.
value is test
{{ "odd" is test }}outputEleven names every template sees without being passed in. _ is an alias of gettext.
Build a list of integers, Python's range.
range(stop) · range(start, stop) · range(start, stop, step)
start intstop intstep int{{ range(1, 4)|list }}{{ range(0, 10, 2)|list }}outputBuild a mapping from pairs, a mapping or named arguments.
dict(pairs) · dict(a=1, b=2)
pairs sequencenamed values{{ dict(a=1, b=2)|items|list }}outputGenerate pseudo-Latin filler text, the same page twice, as safe HTML or plain text.
lipsum(n, html, min, max)
n inthtml boolmin intmax int{{ lipsum(n=1, min=5, max=8) }}outputCycle through items: current names the present item, next advances.
cycler(*items)
items values{% set c = cycler("odd","even") %}{{ c.next() }}{{ c.next() }}{{ c.next() }}outputA separator that prints nothing the first time and the separator after.
joiner(sep)
sep string{% set j = joiner(", ") %}{% for x in ["a","b","c"] %}{{ j() }}{{ x }}{% endfor %}outputA mapping whose fields a template may assign from inside a loop or block.
namespace(**fields)
fields values{% set ns = namespace(n=0) %}{% for i in [1,2,3] %}{% set ns.n = ns.n + i %}{% endfor %}{{ ns.n }}outputTranslate a message through the translation seam; with no seam the text stands.
gettext(message) · _(message)
message string{{ gettext("Hello") }} {{ _("World") }}outputAn alias of gettext.
_(message)
message string{{ _("Hello") }}outputTranslate a singular or plural message by a count.
ngettext(singular, plural, count)
singular stringplural stringcount number{{ ngettext("%(num)d apple", "%(num)d apples", n) }}data{"n":2}outputTranslate a message within a context.
pgettext(context, message)
context stringmessage string{{ pgettext("menu", "Open") }}outputTranslate a singular or plural message within a context.
npgettext(context, singular, plural, count)
context stringsingular stringplural stringcount number{{ npgettext("file", "%(num)d file", "%(num)d files", n) }}data{"n":2}outputWrite the characters of a tag as text, by Django's name for each.
templatetag(tag)
tag string{{ templatetag("openblock") }} if x {{ templatetag("closeblock") }}outputBuild a query string from mappings and named values; null drops a key, a list repeats it.
query_string(*mappings, **values)
mappings mappingvalues values{{ query_string(page=2, q="a b") }}outputThe address a program gives a route. Registered by the program through Env::add_host, so it is shown here rather than run.
url(name, *args, **kwargs)
name stringargs values{{ url("post", 42) }}Needs the program's routes: register url through Env::add_host(host=…). The Guide shows a whole host.
Where a static file is served from. Registered by the program through Env::add_host.
static(path)
path string{{ static("css/site.css") }}Needs the program's static prefix: register asset through Env::add_host(host=…).
The prefix static files sit under, on its own. Registered by the program.
static_prefix()
{{ static_prefix() }}Needs the program's static prefix: register asset_prefix through Env::add_host(host=…).
The prefix uploaded files sit under. Registered by the program.
media_prefix()
{{ media_prefix() }}Needs the program's media prefix: register media_prefix through Env::add_host(host=…).
The token a form carries, as markup. Registered by the program.
csrf_token()
{{ csrf_token() }}Needs the program's token: register csrf_token through Env::add_host(host=…).
The moment to render, written with Django's date letters. Registered by the program: templates read no clock.
now(format)
format string{{ now("Y-m-d H:i") }}Needs the program's clock: register now through Env::add_host(host=…).
The tags that shape a template. Most run live below through the same engine this site ships; a few that need a loader or a host object are shown with their syntax.
Branch on a condition; elif adds a clause, else the fallback.
{% if cond %}…{% elif cond %}…{% else %}…{% endif %}
{% if n > 2 %}big{% elif n == 2 %}two{% else %}small{% endif %}data{"n":2}outputLoop over items. The else runs when there were none; break and continue work inside. The loop variable holds index, index0, revindex, first, last, length, depth, previtem, nextitem, cycle(…) and changed(…); a recursive loop calls loop(children).
{% for x in items %}…{% else %}…{% endfor %}
items sequenceif conditionrecursive flag{% for x in items %}{{ loop.index }}:{{ x }}{% if not loop.last %}, {% endif %}{% else %}none{% endfor %}data{"items":["a","b","c"]}outputBind a name to a value, or to a block of markup with endset.
{% set name = expr %} · {% set name %}…{% endset %}
name nameexpr valueblock flag{% set greeting %}Hello {{ name }}{% endset %}{{ greeting }}data{"name":"Ada"}outputGive names local values for the enclosed block.
{% with a = 1, b = 2 %}…{% endwith %}
names assignments{% with total = 3 %}{{ total }}{% endwith %}outputDefine a reusable block with parameters; call it by name, or with a caller block via endcall.
{% macro name(args) %}…{% endmacro %} · {% call name(args) %}…{% endcall %}
args parameters{% macro field(n, v="") %}<input name="{{ n }}" value="{{ v }}">{% endmacro %}{{ field("q") }}outputApply a filter to a block of markup.
{% filter upper %}…{% endfilter %}
filter filter{% filter upper %}hello {{ name }}{% endfilter %}data{"name":"ada"}outputDefine a named block a parent can fill; super() calls the parent's block, scoped sees the loop around it, required must be filled, append and prepend add to it. The indentation syntax has block append and block prepend too. Needs a loader through Env::new(load=…).
{% extends "base" %} · {% block name %}…{% endblock %}
{% extends "layout.html" %}
{% block body %}page{% endblock %}Inheritance needs a loader, so the example below is shown, not run. The parent is read through load passed to Env::new; the Guide walks a full layout.
Pull in another template; ignore missing tolerates a missing file, with context shares the names, inline renders in place, raw includes plain text, and a block lands where the included file says yield. Needs a loader.
{% include "partial" %} · {% include "partial" ignore missing with context %}
missing flagcontext flaginline flagraw flag{% include "footer.html" ignore missing %}Includes a named template through the loader, so the syntax is shown rather than run here.
Import names from another template as a module, or bring chosen names in directly; with context shares the names. Needs a loader.
{% import "ui" as ui %} · {% from "ui" import card %}
context flag{% from "ui.html" import card %}{{ card("Title", "Note") }}Reads the module through the loader, so the syntax is shown rather than run here.
Write text exactly as written, with no substitution.
{% raw %}…{% endraw %}
{% raw %}{{ not_evaluated }}{% endraw %}outputTurn escaping on or off for the enclosed block.
{% autoescape false %}…{% endautoescape %}
flag bool{% autoescape false %}{{ html }}{% endautoescape %}data{"html":"<b>"}outputWrite the block either way; when the condition is false it is hidden with an inline style, not removed.
{% show cond %}…{% endshow %}
cond bool{% show ok %}<b>shown</b>{% endshow %}data{"ok":true}outputPrint the current context: every name in scope.
{% debug %}
{% debug %}outputMark text for translation; pluralize switches on a count. With no translation seam the source text stands.
{% trans %}…{% endtrans %} · {% trans count=n %}…{% pluralize %}…{% endtrans %}
count numbercontext string{% trans count=n %}{{ count }} item{% pluralize %}{{ count }} items{% endtrans %}data{"n":3}outputEvaluate an expression for its side effect, typically calling a method of a host object.
{% do object.method() %}
expr call{% do user.log("visited") %}outputRender a block once and, with a cache passed to Env::new, keep it; without one it renders every time.
{% once key %}…{% endonce %}
key value{% once "sidebar" %}{{ 1 + 1 }}{% endonce %}output