django v1.6 - 在一个循环中使用循环标签两次

django v1.6 - using cycle tag twice in a single loop

我使用的是 django 1.6 版。我有一个 for 循环,需要 {% cycle %} 模板标签为每个循环分配不同的值,一个在 class 级别,另一个在图像源。

{% for item in items %}
   <article class={% cycle '"entry style-grid style-hero"' '"entry style"' ... %}>
   .... some more code ...
   <img src={% cycle '"/static/file.png/"' '"/static/file1.png/"' %}>
{% endfror %}

第一个循环似乎工作正常,但第二个循环不起作用。 html 源代码显示了整个循环块,包括第二个循环块的标签。

我也使用了 {% load cycle from future %} 这是 1.6 版本的新功能,但仍然无法正常工作。

不确定如何让它工作。

编辑:

{% for item in items %}
            <article class={% cycle '"entry style-grid style-hero hero-sm-largest type-post col-sm-12 col-md-6 col-lg-6 col-xl-6 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' '"entry style-grid style-hero type-post col-sm-12 col-md-3 col-lg-3 col-xl-3 colheight-sm-1 colheight-md-2 colheight-lg-2 colheight-xl-2"' %}>

                <div class="ribbon ribbon-pulled ribbon-small ribbon-highlight">
                    <a href="{{item.slug}}">{{ item.name}} </a>
                    <a href="{{item.slug}}"><img src="{{ item.country}}" alt="{{item.country}}" /></a>
                </div>

                <header class="entry-header">
                    <h3 class="entry-title"><a href="{{item.slug}}">{{item.name}}</a> </h3>
                    <div class="entry-meta">
                        <span class="entry-date"><a href="{{item.slug}}">
                        {{item.profession}}</a></span>
                    </div>
                </header>

                <figure class="entry-thumbnail">

                    <a href="{{item.slug}}" class="overlay overlay-primary"></a>

                    <!-- to disable lazy loading, remove data-src and data-src-retina -->


                         <img src={% cycle '"/static/placeholder.gif" data-src="/media/{{item.picture}}" data-src-retina="/media/{{item.picture}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"' 
                         '"/static/placeholder.gif" data-src="/media/{{item.picture_small}}" data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}"'  %}>

                    <noscript>
                        <img src="/media/{{item.picture}}" alt="">
                    </noscript>

                </figure>
            </article>
                {% endfor %}
        </div>

{% cycle %} 模板标签不能是多行的。加入到单行

但无论如何您的代码将无法工作 - {{ variable }} 不会在 {% cycle %} 标记中展开。你应该在这里使用 {% if %} 标签:

{% if forloop.counter0|divisibleby:"11" %}
   <img src="/static/placeholder.gif" data-src="/media/{{item.picture}}"
           data-src-retina="/media/{{item.picture}}" alt="{{item.name}}">
{% else %}
   <img src="/static/placeholder.gif" data-src="/media/{{item.picture_small}}"
           data-src-retina="/media/{{item.picture_small}}" alt="{{item.name}}">
{% endif %}