如何在django模板中循环2 for for循环

时间:2013-10-17 20:03:38

标签: django django-templates

我有一个django模板,我需要在两个不同的for循环中循环显示一组背景颜色。循环标记似乎被设计为在一个for循环内或在for循环外使用。这是我的代码:

{% if global_adverts %}
        <span style="display:none">{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors %}</span>
        {% for advert in global_adverts %}
            <div class="{% cycle adcolors %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle adcolors %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

有没有办法在第一个循环之前输出循环中的第一个项目而不得不用css隐藏它?

3 个答案:

答案 0 :(得分:1)

只需添加无声

即可
{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors silent %}

它可以防止标记输出值

评论后编辑

如果您需要为每个循环继续循环,只需使用循环两次并更改第二个循环中项目的顺序:

{% if global_adverts %}
        {% for advert in global_adverts %}
            <div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle 'advert-pale-blue' 'advert-green' 'advert-blue' 'advert-grey' %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

答案 1 :(得分:1)

我发现以下代码给出了所需的结果:

{% if global_adverts %}
        {% for advert in global_adverts %}
            <div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolours %}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
        {% endfor %}
    {% endif %}
    {% with self.adverts.all as adverts %}
        {% if adverts %}
            {% for advert in adverts %}
                <div class="{% cycle adcolours %}">
                    {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                        <p>{{ advert.text }}</p>
                    {% if advert.url %}</a>{% endif %}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

答案 2 :(得分:0)

我觉得在循环外使用第一个{% cycle %}声明更干净,我不希望在 conditional 循环内使用声明,这是如何实现的工作:

{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolour silent %}

{% if global_adverts %}
    {% for advert in global_adverts %}
        <div class="{{ adcolours }}">
            {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                <p>{{ advert.text }}</p>
            {% if advert.url %}</a>{% endif %}
        </div>
        {% cycle adcolours %}
    {% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
    {% if adverts %}
        {% for advert in adverts %}
            <div class="{{ adcolours }}">
                {% if advert.url %}<a href="{{ advert.url }}">{% endif %}
                    <p>{{ advert.text }}</p>
                {% if advert.url %}</a>{% endif %}
            </div>
            {% cycle adcolour %}
        {% endfor %}
    {% endif %}
{% endwith %}

由于{% cycle adcolour %}是无声的,因此我们使用{{ adcolour }}打印当前颜色。我们仍然需要使用无声的{% cycle adcolour %}来逐步遍历每次迭代的颜色。