2

如果我有一个看起来像这样的 forloop:

{% for field in form  %}
  {{ field }}
{% endfor %}

如何修改它以排除循环中的第一个条目?我已经尝试了一些东西,但还没有奏效。例如:

{% for field in form  %}
{% if field != 1 %}   #also {% if field.id != 1 %}
  {{ field }}
{% endif %}
{% endfor %}

也试过:

{% if form.field != 1 %}
{% for field in form  %}
  {{ field }}
{% endif %}
{% endfor %}

或者有没有办法在我的 forms.py 或 views.py 中排除这种相对简单的方法?我正在使用表单向导,所以我的视图和表单已经非常复杂......但我很感激任何建议!

4

1 回答 1

3

forloop.first

{% for field in form  %}
    {% if not forloop.first %}          {#  <-- exclude the first entry #}
        {{ field }}
    {% endif %}
{% endfor %}

作为参考,请在此处查看 forloop 中可用的所有其他变量... https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatetag-for

于 2013-10-02T04:01:23.393 回答