如何检查Django模板中集合的大小? [英] How can I check the size of a collection within a Django template?
本文介绍了如何检查Django模板中集合的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Django模板中有一个列表。仅在列表大小大于零时,我才想做某事。
I have a list in my Django template. I want to do something only if the size of the list is greater than zero.
我尝试了 myList | length
和 myList | length_is
,但没有成功。
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?
推荐答案
请参见 https://docs.djangoproject.com/en/stable/ref/templates/builtins/#if :仅用于复制其示例:
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 %}
这篇关于如何检查Django模板中集合的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文