如何使用Jinja2迭代WTForms FieldList

时间:2013-06-19 22:47:54

标签: jinja2 wtforms

正如标题所说,这就是我所拥有的:

form = F(obj = myobject)
myfieldlist= FieldList(FormField(form))

{% for subfield in form.myfieldlist %}
    {{ subfield.field }}
    {{ subfield.label }}
{% endfor %}

这没有输出任何想法?此外,还不完全确定是否需要FormField。 感谢

1 个答案:

答案 0 :(得分:15)

FormField认为不是实例:

class GuestForm(Form):
    email = TextField()
    vip = BooleanField()

class VenueForm(Form):
    name = TextField()
    guests = FieldList(FormField(GuestForm))

然后在你的控制器中:

form = VenueForm(obj=myobject)
render("template-name.html", form=form)

在模板中,您需要迭代FieldList字段,就好像它是自己的形式一样:

{% for guest_form in form.guests %}
    <ul>
    {% for subfield in guest_form %}
    <li>{{ subfield.label }} {{ subfield }}</li>
    {% endfor %}
    </ul>
{% endfor %}