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

前端 未结 7 1521
南旧
南旧 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:58

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

提交回复
热议问题