【问题标题】:Flask python loop.index modulo烧瓶 python loop.index 模
【发布时间】:2014-07-10 09:27:12
【问题描述】:

您好,我正在尝试列出一些元素,这些元素在每个第三个元素之后被一个 clearfix div 划分。这是我尝试过的不起作用。如何在烧瓶中做模数?

{% for i in props %}
<li class="col-xs-4">
    <label class="basic-medium-black">
         <span class="icon-check {% if i in user.props %}active{% endif %}"></span>
         {% filter upper %}{{ i.prop_name }} {% endfilter %}
    </label>
</li>

****************This Part:******
{% if loop.index % 3 == 0 %}
    <div class="clearfix"></div>
{% endif %}
********************************

{% endfor %}

我应该为“%”的模数输入什么?

提前谢谢..

【问题讨论】:

  • 您在寻找与以下不同的答案吗?

标签: python flask modulo werkzeug


【解决方案1】:

我会使用内置变量测试divisibleby,如下所示。

{% if loop.index is divisibleby 3 %}

{% if loop.index is divisibleby(3) %}

【讨论】:

    【解决方案2】:

    您也可以使用batch() filter 对您的项目进行分组,而不是使用模数,这样您就可以在每个组之后进行明确的修复:

    {% for row in props|batch(3) %}
        {% for i in row %}
        <li class="col-xs-4">
            <label class="basic-medium-black">
                 <span class="icon-check {% if i in user.props %}active{% endif %}"></span>
                 {% filter upper %}{{ i.prop_name }} {% endfilter %}
            </label>
        </li>
        {% endfor %}
    
        <div class="clearfix"></div>        
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多