Flask + WTForms - 在FormList中显示字段

时间:2014-06-06 17:45:57

标签: flask jinja2 wtforms flask-wtforms

我正在尝试使用动态字段构建表单(单击加号以添加额外字段)。

forms.py

class ActionForm(Form):
    key = SelectField("Type: ", coerce=int, choices=[(0, "Option 1"), (1, "Option 2"), (2, "Opeion 3")], default=0)
    value = StringField('Value: ')


class EditForm(Form):
    content = StringField("Content: ")
    actions = FieldList(FormField(ActionForm))
    status = RadioField("Status: ", coerce=int, choices=[(0, "Inactive"), (1, "Active")], default=1)
    submit = SubmitField("Submit")

查看模板(不会从ActionForm渲染字段):

<form method="POST">
{{ form.csrf_token }}
{{ form.actions.label }}
<div class="form-group input-group">
    {% for action in form.actions %}
        {% for field in action %}
            {{ field() }}
        {% endfor %}
    {% endfor %}
</div>
{{ form.status.label }}{{ form.status }}
{{ form.submit() }}
</form>

问题:

在我的表单中,我只看到一个空白点,ActionForm字段应该出现。

换句话说,我无法遍历form.actions(以显示SelectField()和StringField())。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

FieldList采用min_entries关键字参数 - 如果您设置它,它将确保至少有n个条目:

class EditForm(Form):
    content = StringField("Content: ")
    actions = FieldList(FormField(ActionForm), min_entries=1)