199

I have a list in my Django template. I want to do something only if the size of the list is greater than zero.

I have tried myList|length and myList|length_is but they have not been successful.

I've searched all over and don't see any examples. How can I check this?

daaawx
  • 3,273
  • 2
  • 17
  • 16
MrDatabase
  • 43,245
  • 41
  • 111
  • 153

8 Answers8

370

See https://docs.djangoproject.com/en/stable/ref/templates/builtins/#if : just use, to reproduce their example:

{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% else %}
    No athletes.
{% endif %}
MichielB
  • 4,181
  • 1
  • 30
  • 39
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
128

If you're using a recent Django, changelist 9530 introduced an {% empty %} block, allowing you to write

{% for athlete in athlete_list %}
  ...
{% empty %}
  No athletes
{% endfor %}

Useful when the something that you want to do involves special treatment for lists that might be empty.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
28

A list is considered to be False if it has no elements, so you can do something like this:

{% if mylist %}
    <p>I have a list!</p>
{% else %}
    <p>I don't have a list!</p>
{% endif %}
mipadi
  • 398,885
  • 90
  • 523
  • 479
13

If you tried myList|length and myList|length_is and its not getting desired results, then you should use myList.count

Nilesh Tighare
  • 915
  • 1
  • 9
  • 22
  • 2
    I'm shocked at how many upvotes this has... If `myList` is a `list` then this calls `list.count()` which raises an error that's masked in the template and gets treated as a false value. The `count` method is to count the number of instances that match the passed in value. However, this works if `myList` is a queryset. – Tim Tisdall Mar 17 '21 at 17:53
  • Yes, for a collection `|length` can be used. For a QuerySet `.count` is adapted; you could use `exists` as well. – François Constant Apr 25 '23 at 23:47
9

You can try with:

{% if theList.object_list.count > 0 %}
    blah, blah...
{% else %}
    blah, blah....
{% endif %} 
Atarx
  • 91
  • 1
  • 6
8

This works:

{% if myList|length %}
    Do something!
{% endif %}

Why there's so many answers here and why there's so much confusion is that this didn't always work. I think at one point template filters couldn't be used on arguments to the if statement and this was later added. It's also now possible to do things such as {% if myList|length >= 3 %}. The filter should do the equivalent of len(myList) so any type of object that can handle that would also be able to handle the |length filter.

Tim Tisdall
  • 9,914
  • 3
  • 52
  • 82
1

Collection.count no bracket

{% if request.user.is_authenticated %}
{{wishlists.count}}
{% else %}0{% endif %}
Ahmed Adewale
  • 2,943
  • 23
  • 19
1

I need the collection length to decide whether I should render table <thead></thead>

but don't know why @Django 2.1.7 the chosen answer will fail(empty) my forloop afterward.

I got to use {% if forloop.first %} {% endif %} to overcome:

<table>
    {% for record in service_list %}
        {% if forloop.first %}
            <thead>
            <tr>
                <th>日期</th>
            </tr>
            </thead>
        {% endif %}
        <tbody>
        <tr>
            <td>{{ record.date }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
C.K.
  • 4,348
  • 29
  • 43