用于循环并显示前X个匹配项的Django模板 [英] Django template for loop and display first X matches
本文介绍了用于循环并显示前X个匹配项的Django模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不确定确切地表达我的问题的方式,但是我本质上是想遍历一个列表并仅显示前4个匹配项.
Not sure exactly how to phrase my problem but I essentially want to loop through a list and show the first 4 matches only.
{% for reward_type in reward_types %}
<h2>{{ reward_type.name }}</h2>
<div class="reward_category">
{% for category in reward_categories %}
{% if category.reward_type == reward_type %}
.
.
Show the first 4 matches
.
.
{% endif %}
{% endfor %}
</div>
{% endfor %}
推荐答案
在将列表传递到模板之前,请使用视图函数准备列表.
Use your view function to prepare your list before passing it to the template.
Django模板不适用于像这样的复杂匹配.
Django template isn't intended for complex matching like this.
完全如所讨论的;-)
这篇关于用于循环并显示前X个匹配项的Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文