10

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

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

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

这什么也没输出,有什么想法吗?此外,不完全确定是否需要 FormField。谢谢

4

1 回答 1

17

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 %}
于 2013-06-26T01:49:33.900 回答