【问题标题】:No named cycles in template. 'row1,row2' is not defined模板中没有命名循环。 'row1,row2' 未定义
【发布时间】:2016-11-15 07:06:18
【问题描述】:

在我的 django 内联表单集中,表单 html:

{% block body %}        
    <h2>Profile</h2>
    <hr>
    <div class="col-md-4">
        <form action="" method="post">{% csrf_token %}
            {{ form.as_p }}

            <table class="table">
                {{ familymembers.management_form }}

                {% for form in familymembers.forms %}
                    {% if forloop.first %}
                        <thead>
                        <tr>
                            {% for field in form.visible_fields %}
                                <th>{{ field.label|capfirst }}</th>
                            {% endfor %}
                        </tr>
                        </thead>
                    {% endif %}
                    <tr class="{% cycle row1,row2 %} formset_row">
                        {% for field in form.visible_fields %}
                            <td>
                                {# Include the hidden fields in the form #}
                                {% if forloop.first %}
                                    {% for hidden in form.hidden_fields %}
                                        {{ hidden }}
                                    {% endfor %}
                                {% endif %}
                                {{ field.errors.as_ul }}
                                {{ field }}
                            </td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </table>
            <input type="submit" value="Save"/> <a href="{% url 'profile-list' %}">back to the list</a>
        </form>
    </div>
{% endblock %}

当我尝试打开表单时,它给出了

/profile/add/ 处的模板语法错误 模板中没有命名循环。 'row1,row2' 没有定义

我怎样才能避免这个错误?

【问题讨论】:

    标签: django templates


    【解决方案1】:

    这不是您使用该标签的方式,如the docs 所示。这些值应该用空格而不是逗号分隔,如果它们是文字字符串,它们应该用引号引起来。

    {% cycle "row1" "row2" %} 
    

    【讨论】:

    • FWIW。这就是您在早期版本中使用该标签的方式。所以这可能是升级到新版本 Django 的问题。
    【解决方案2】:

    如果还是报错,可以试试:

    class="{% cycle 'row1' 'row2' %} formset_row"
    

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多