[Django]-Django template: check for empty query set

51๐Ÿ‘

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 %}

๐Ÿ‘คThales Ceolin

39๐Ÿ‘

Try {% if notes.all %}. It works for me.

๐Ÿ‘คMiketsukami

8๐Ÿ‘

Itโ€™s unfortunate that youโ€™re stuck using a raw query set โ€“ theyโ€™re missing a lot of useful behavior.

You could convert the raw query set into a list in the view:

notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))

Then check it as a boolean in the template:

{% if notes %}
    Header
    {% for note in notes %}
        {{ note.text }}
    {% endfor %}
{% endif %}

You could also make it happen without conversions using forloop.first:

{% for note in notes %}
    {% if forloop.first %}
         Header
    {% endif %}
    {{ note.text }}
{% endfor %}
๐Ÿ‘คPeter DeGlopper

6๐Ÿ‘

In your view check whether notes is empty or not. If it is then you pass None instead:

{"notes": None}

In your template you use {% if notes %} as normal.

๐Ÿ‘คSimeon Visser

4๐Ÿ‘

What about:

{% if notes != None %}
    {% if notes %}
        NOTES:
        {% for note in notes %}
            {{ note.text }}  
        {% endfor  %}
    {% endif %}
{% else %}
    NO NOTES AT ALL
{% endif %}
๐Ÿ‘คmatino

3๐Ÿ‘

Your original solution

{% if notes %}
    Header
    {% for note in notes %}
        {{ note.text }}
    {% endfor %}
{% endif %}

Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.

๐Ÿ‘คLaszlo Marai

3๐Ÿ‘

Often the right way to do this is to use the {% with ... %} tag. This caches the query so it runs only once and also gives you more flexibility with your markup than using {% empty %}.

{% with notes as my_notes %}
{% if my_notes %}
<ul>
  {% for note in my_notes %}
  <li>{{ note }}</li>
  {% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}

With this particular example Iโ€™m not sure how useful it is but if youโ€™re querying Many-to-Many field, for instance, itโ€™s likely what you want to do.

๐Ÿ‘คgetup8

1๐Ÿ‘

Use {% empty %} in django templates

{% if list_data %}
    {% for data in list_data %}
        {{ data.field_1 }}
    {% endfor %}
{% else %}
    <p>No data found!</p>
{% endif %}

We can write above code with {% empty %}.

{% for data in list_data %}
    {{ data.field_1 }}
{% empty %}
    <p>No data found!</p>
{% endfor %}
๐Ÿ‘คRamlakhan Kevat

0๐Ÿ‘

you can use the {% empty %} for a "for loop" as in:

{% for object in list %}
<p>{{ object }}</p>
{% empty %}
<p>list is empty</p>
{% endfor %}

use โ€˜.allโ€™

{% if object.something.all %}
๐Ÿ‘คLateef Taiwo

Leave a comment