Django template: check for empty query set

Have a look at the {% empty %} tag.
Example from the documentation

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>

Link: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty

If you are interested in a table, or some kind of heading if there are results, add the forloop.first:

    {% for athlete in athlete_list %}
    {% if forloop.first %}
    Athlete Name:
    {% endif %}

  • {{ athlete.name }}
  • {% empty %}

  • Sorry, no athletes in this list.
  • {% endfor %}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)