开发者

python add a new div every 3rd iteration

开发者 https://www.devze.com 2022-12-14 11:23 出处:网络
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

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">&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. 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 %}


Use forloop.counter and a modulo operator inside the loop:

{% 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

EDIT:

Fixed the code example.

0

精彩评论

暂无评论...
验证码 换一张
取 消