How can I check the size of a collection within a Django template?

前端 未结 7 1524
南旧
南旧 2020-12-12 21:31

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 m

7条回答
  •  情深已故
    2020-12-12 21:49

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

    {% if mylist %}
        

    I have a list!

    {% else %}

    I don't have a list!

    {% endif %}

提交回复
热议问题