default Filter

Logic Most Useful

Argument: default (required) – the default value.

Documentation

Used to provide a default string when the variable evaluates to False.

Variable

inventory = {
    'gloves': 0,
    'hats': 51,
    'scarves': 2,
    'socks': 13
}

Template

<ol>
  {% for item, remaining in inventory.items %}
    <li>{{ item }}: {{ remaining|default:"out of stock" }}</li>
  {% endfor %}
</ol>

Result

<ol>
  <li>gloves: out of stock</li>
  <li>hats: 51</li>
  <li>scarves: 2</li>
  <li>socks: 13</li>
</ol>

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

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST