About Generators in Django Templates

In Django templates, when you use generators and need the empty list condition, use the {% empty %} template tag:

1
2
3
4
5
{% for item in item_generator %}
    {{ item }}<br />
{% empty %}
    No items.
{% endfor %}

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2