在if条件满足3次后,如何使for product in products
环路断开.我一直在try 设置一个计数器,但这不起作用...因为set
在for循环内不被接受.尽管对它进行了更多的测试,但它在任何地方都不被接受.
当我使用set
时,它抛出这个异常或其变体:Invalid block tag on line 11: 'set', expected 'endblock'. Did you forget to register or load this tag?
我知道我应该把我正在使用的模板的所有逻辑放在视图内部,然后通过字典传递它,但研究这方面的所有内容需要花费太多时间,我只想完成这项工作.
老实说,我真的很累,现在是早上6点,我不知道该怎么办.提前感谢您的帮助.:)
编辑:我知道我必须使用namespace()使set能够跨作用域传播.但set
本身仍引发了与上述相同的例外.
Edit2:我认为django使用jinja2作为模板语言,但事实似乎并非如此.我修复了提到jinja2的问题,因为我没有改变新安装的django附带的任何默认设置.
HTML
{% for category in categories %}
<div>
<h5 class="text-line"><a href="" class="store-category"><span>{{category.name}}</span></a></h5>
</div>
<!-- Cards Container -->
<div class="shop-cards">
{% set counter = 0 %}
{% for product in products %}
{% if product.category.categoryID == category.categoryID %}
<!-- CARD 1 -->
<div class="card">
<image class="product-image" src="{% static 'images/product-4.png' %}"></image>
<div class="card-category-price">
<h3 class="product-name">{{product.name}}</h3>
<h3 class="product-price">{{product.price}}</h3>
</div>
</div>
{% endif %}
{% endfor %}
</div>
{% endfor %}