default Filter

Logic Most Useful

Argument: default (required) – the default value.


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


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


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


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

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