如何在django html中创建计数器?
我试图用一个计数器告诉我,如果dato.tipo_cake==categoria为真,那么第一次有多少次,这样我可以在这个值为12和24时设置代码行,但是唯一有计数器的是for,它计算所有的项,而不是有一种属性的项如何在django html中创建计数器?,html,django,counter,Html,Django,Counter,我试图用一个计数器告诉我,如果dato.tipo_cake==categoria为真,那么第一次有多少次,这样我可以在这个值为12和24时设置代码行,但是唯一有计数器的是for,它计算所有的项,而不是有一种属性的项 {% for dato in cakes %} {% if dato.tipo_cake == categoria %} <div class="col-md-3 col-sm-6" ><a href="{{ MEDIA_URL }}{{ d
{% for dato in cakes %}
{% if dato.tipo_cake == categoria %}
<div class="col-md-3 col-sm-6" ><a href="{{ MEDIA_URL }}{{ dato.imagen }}" class="thumbnail" title="{{ dato.titulo }}" data-gallery="blueimp-gallery"><img class="img-responsive" src="{{ MEDIA_URL }}{{ dato.thumbnail }}" alt="Thumb_{{ dato.titulo }}" width="240"></a>
</div>
{% if forloop.counter == 12 %}
{{ forloop.counter }}
</div>
</div>
<div class="item">
<div class="row">
{% endif %}
{% if forloop.counter == 24 %}
</div>
</div>
<div class="item">
<div class="row">
{% endif %}
{% endif %}
{% endfor %}
您不能在模板上执行任何算术运算,但可以在views.py上执行。因此,现在您的代码应该如下所示:
#views.py
def counter(request):
n = 0
for dato in cakes:
if dato.tipo_cake == categoria:
n += 1
if n == 12:
return render(request, 'Your_template.html', {"Counter_value":n})
elif n == 24:
# Return something also
这段代码是图库的一部分,根据tipo_蛋糕的不同,我可以显示它,我需要知道这个条件是真实的多少次,所以我只能有12个项目,同时你没有读这篇文章,是吗。很抱歉,我不知道你的评论是一个链接,现在我看到了。我仍然需要一些东西来帮助我知道我显示该链接/imagen的次数。为什么要在view.py中进行计算,然后将信息发送到模板。