Django v1.6 - 在单个循环中使用循环标签两次



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

{% 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 源代码显示整个循环块,包括第二个循环块的标签。

我还使用了 {% 来自未来 %%} 的负载周期,这是 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 %}

最新更新