python每第三次迭代添加一个新的div [英] python add a new div every 3rd iteration
本文介绍了python每第三次迭代添加一个新的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个产品列表,将 3 个产品放在一行并清除该行并添加另外 3 个,这在任何地方都可以正常工作,但 IE6,我知道在每组周围添加 <div>
3个产品会解决这个是目前的模板文件
{% for product in category.products.all %}<div class="{% cycle 'clear' '' '' %}"><a href="{% url shop.views.product category.slug product.slug %}"><img src="{{MEDIA_URL}}{{product.mini_thumbnail}}" alt="{{product.name}}" class="thumbnail"/></a><div class="prod-details"><h3><a href="{% url shop.views.product category.slug product.slug %}">{{product.get_product_name}}</a></h3><h4 class="strap">{{product.get_product_detail}}</h4><p>{{product.strap }}</p><ul><li class="price">£{{product.price}}</li><select name="quantity_{{product.id}}"><option label="1" value="1">1</option><option label="2" value="2">2</option><option label="3" value="3">3</option><option label="4" value="4">4</option><option label="5" value="5">5</option><option label="6" value="6">6</option><option label="7" value="7">7</option><option label="8" value="8">8</option><option label="9" value="9">9</option></选择> <li><a href="{% url shop.views.product category.slug product.slug %}">详情></a></li><li class="right"><input type="submit" name="add_to_basket_{{product.id}}" value="Add to Basket >"/></li>
{% 结束为 %}
解决方案
codeape 的解决方案仅在您使用 Django 主干的最新 SVN 检出时才有效.如果您使用的是 1.1 或更低版本,则不支持该语法.
相反,您可以使用 divisibleby
过滤器:
{% if forloop.counter|divisibleby:3 %}{% endif %}I have a product list that put 3 products on a row and clears the row and adds another 3, this works fine everywhere but IE6, i know that adding <div>
around each group of 3 products will solve this is the template file at the moment
{% for product in category.products.all %}
<div class="{% cycle 'clear' '' '' %}">
<a href="{% url shop.views.product category.slug product.slug %}"><img src="{{MEDIA_URL}}{{product.mini_thumbnail}}" alt="{{product.name}}" class="thumbnail"/></a>
<div class="prod-details">
<h3><a href="{% url shop.views.product category.slug product.slug %}">{{product.get_product_name}}</a></h3>
<h4 class="strap">{{product.get_product_detail}}</h4>
<p>{{ product.strap }}</p>
<ul>
<li class="price">£{{product.price}}</li>
<li class="quantity">
<select name="quantity_{{product.id}}">
<option label="1" value="1">1</option>
<option label="2" value="2">2</option>
<option label="3" value="3">3</option>
<option label="4" value="4">4</option>
<option label="5" value="5">5</option>
<option label="6" value="6">6</option>
<option label="7" value="7">7</option>
<option label="8" value="8">8</option>
<option label="9" value="9">9</option>
</select>
</li>
<li><a href="{% url shop.views.product category.slug product.slug %}">Details ></a></li>
<li class="right"><input type="submit" name="add_to_basket_{{product.id}}" value="Add to Basket >"/></li>
</ul>
</div>
</div>
{% endfor %}
解决方案 codeape's solution only works if you are using a very recent SVN checkout of Django trunk. If you're using version 1.1 or below, that syntax is not supported.
Instead, you can use the divisibleby
filter:
{% if forloop.counter|divisibleby:3 %}<div>{% endif %}
这篇关于python每第三次迭代添加一个新的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文