default_if_none Filter

Logic

Argument: default (required) – the default value.

Documentation

Outputs default if and only if the value is None.

Variables

a = None
b = 0
c = ''
d = False
e = []

Template

<ol>
  <li>{{ a|default_if_none:"No a" }}</li>
  <li>{{ b|default_if_none:"No b" }}</li>
  <li>{{ c|default_if_none:"No c" }}</li>
  <li>{{ d|default_if_none:"No d" }}</li>
  <li>{{ e|default_if_none:"No e" }}</li>
</ol>

Result

<ol>
  <li>No a</li>
  <li>0</li>
  <li></li>
  <li>False</li>
  <li>[]</li>
</ol>

Notice that the default is only used for a. While all the rest evaluate to False, they are not None.


Did we get something wrong? Is there a use case for the default_if_none filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Sept. 16, 2020, 7:34 p.m. EST