简体   繁体   English

python每第三次迭代添加一个新的div

[英]python add a new div every 3rd iteration

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我有一个产品列表,将 3 个产品放在一行并清除该行并添加另外 3 个,这在任何地方都可以正常工作,但 IE6,我知道在每组 3 个产品周围添加<div>将解决这是模板文件瞬间

{% 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">&pound;{{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 &gt;</a></li>
                <li class="right"><input type="submit" name="add_to_basket_{{product.id}}" value="Add to Basket &gt;"/></li>
            </ul>
            </div>
        </div>
    {% endfor %}

codeape's solution only works if you are using a very recent SVN checkout of Django trunk. codeape 的解决方案仅在您使用 Django 主干的最新 SVN 结帐时才有效。 If you're using version 1.1 or below, that syntax is not supported.如果您使用的是 1.1 或更低版本,则不支持该语法。

Instead, you can use the divisibleby filter:相反,您可以使用divisibleby过滤器:

{% if forloop.counter|divisibleby:3 %}<div>{% endif %}

Use forloop.counter and a modulo operator inside the loop:在循环内使用forloop.counter和模运算符:

{% for ... %}
    {% if forloop.counter|divisibleby:3 %}<div>{% endif %}
    ...
    {% if forloop.counter|divisibleby:3 %}</div>{% endif %}
{% endfor %}

See http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for请参阅http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

EDIT:编辑:

Fixed the code example.修复了代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM