Template Filter - default
Example
Write color names, but write "nocolor" if the value is empty:
{% for x in colors %}
<h1>{{ x|default:"nocolor" }}</h1>
{% endfor %}
Run Example »
Definition and Usage
The default
filter allows you to specify a
default value to use if the value evaluates to False
.
Values that evaluates to False
are:
- Empty Strings
""
- Any numeric
0
- Any empty object
[]
,()
,{}
,set()
,range(0)
None
False
Syntax
{{ value|default:defaultvalue }}
Template filters are defined by using a pipe |
character followed by the name of the filter.
Arguments are defined by using a colon :
character followed by the argument value.
Arguments
Value | Description |
---|---|
defaultvalue | Required. The value to return if the value is False . |