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

前端 未结 7 1535
南旧
南旧 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:56

    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.

提交回复
热议问题