循环模板标签仅适用于固定数量的参数
Cycle template tag only works with fixed number of arguments
我正在使用模板标签 cycle 将不同的 css class 添加到依赖于 Django 模板中循环索引的元素。请参阅以下示例:
{% for id in menus %}
<div class="col-md-2 {% cycle 'col-md-offset-1' %}">
</div>
{% endfor %}
这会引发 No named cycles in template. ''col-md-offset-1'' is not defined
错误。
menus
列表有 5 个条目,如果我将 cycle
方法的参数数量调整为它起作用的列表条目数量:
{% for id in menus %}
<div class="col-md-2 {% cycle 'col-md-offset-1' '' '' '' ''%}">
</div>
{% endfor %}
循环模板标签是否总是需要与使用的列表具有准确数量的参数?这对我来说听起来不对。
是否有不同的方法来为第一个元素设置 class?
我不明白你为什么只对一件事使用循环。这没有意义。
如果只想为第一个元素设置 class,请使用 {% if forloop.first %}
。
我正在使用模板标签 cycle 将不同的 css class 添加到依赖于 Django 模板中循环索引的元素。请参阅以下示例:
{% for id in menus %}
<div class="col-md-2 {% cycle 'col-md-offset-1' %}">
</div>
{% endfor %}
这会引发 No named cycles in template. ''col-md-offset-1'' is not defined
错误。
menus
列表有 5 个条目,如果我将 cycle
方法的参数数量调整为它起作用的列表条目数量:
{% for id in menus %}
<div class="col-md-2 {% cycle 'col-md-offset-1' '' '' '' ''%}">
</div>
{% endfor %}
循环模板标签是否总是需要与使用的列表具有准确数量的参数?这对我来说听起来不对。
是否有不同的方法来为第一个元素设置 class?
我不明白你为什么只对一件事使用循环。这没有意义。
如果只想为第一个元素设置 class,请使用 {% if forloop.first %}
。