【问题标题】:django v1.6 - using cycle tag twice in a single loopdjango v1.6 - 在一个循环中使用两次循环标签
【发布时间】:2015-01-27 05:24:42
【问题描述】:

我正在使用 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 源代码显示了整个循环块,包括第二个的标签。

我还使用了 {% 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>

【问题讨论】:

  • 你能显示{% for %}循环的全部内容吗?还有一个语法错误({% endfror %} 而不是{% endfor %})。
  • @catavaran 我肯定会把它贴在上面。对不起,这是一个错字,但在 html 页面上是正确的。
  • @catavaran 嘿刚刚粘贴在上面。谢谢

标签: django django-templates django-views cycle


【解决方案1】:

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

【讨论】:

  • 感谢它的工作。但问题是标签内的值没有呈现。即 /media/{{item.picture_small}} 不会呈现实际位置和文件名。 html 源代码按原样打印
  • 请注意答案中的{% if %} 选项。
  • 感谢您的回复。我在没有刷新屏幕的情况下发表了评论。原来你已经为我遇到的下一个问题给出了答案。先生,您对这一点很彻底。非常感谢。
猜你喜欢
  • 2020-10-17
  • 1970-01-01
  • 2013-11-13
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 2017-03-01
相关资源
最近更新 更多