【发布时间】:2021-05-07 14:58:06
【问题描述】:
我希望我的 for 循环在 if 条件满足三次后结束,同时在我的电子商务网站中呈现我的“产品”详细信息。
category.html
{% for item in subcategory %}
{% with counter="0" %}
<div>
<h5 class="my-0" style="font-weight: 700;">{{ item.title }}</h3>
</div>
<div class="row">
{% for i in products %}
{% if i.category.id == item.id %}
{{ counter|add:"1" }}
{% if counter == "3" %}
{{ break}}
{% endif %}
<div class="col-3 p-0 px-1 my-3">
<a href="{% url 'product_detail' i.id i.slug %}">
<img class="w-100" id="catbox" src="{{ i.image.url }}" alt="">
</a>
</div>
{% endif %}
{% endfor %}
<div class="col-3 p-0 my-3 text-center"
style="border-radius: 10px;background: url({% static 'images/temp.jpeg' %}); background-size: cover;">
<div class="h-100 w-100 m-0 p-0" style="background: rgba(0, 0, 0, 0.61);border-radius: 10px;">
<div style="position: relative;top: 50%;transform: translateY(-50%);">
<a href="{% url 'category_product' item.id item.slug %}" class="text-white " href="#"><strong>SEE
ALL</strong></a>
</div>
</div>
</div>
</div>
{% endwith %}
{% endfor %}
我希望第二个 for 循环,即 {% for i in products %},一旦满足 if 条件,即其中的 {% if i.category.id == item.id %},就中断 三次。但是我设置为 0 的计数器会重复递增到 1,而不是使用 for 循环重复递增。由于里面有一个 if 条件,我也不能使用 forloop.counter 。
我希望对第一个 for 循环的每次迭代重复此过程,即 {% for item in subcategory %}
【问题讨论】:
标签: python html django for-loop django-templates