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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1945379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:24:56  来源:igfitidea点击:

python add a new div every 3rd iteration

pythondjango-templates

提问by Udders

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,我知道<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">&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 %}

回答by Daniel Roseman

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.

codeape 的解决方案仅在您使用 Django 主干的最新 SVN 检出时才有效。如果您使用的是 1.1 或更低版本,则不支持该语法。

Instead, you can use the divisiblebyfilter:

相反,您可以使用divisibleby过滤器:

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

回答by codeape

Use forloop.counterand 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.

修复了代码示例。