我正在尝试使用动态字段构建表单(单击加号以添加额外字段)。
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())。
我做错了什么?
答案 0 :(得分:1)
FieldList
采用min_entries
关键字参数 - 如果您设置它,它将确保至少有n
个条目:
class EditForm(Form):
content = StringField("Content: ")
actions = FieldList(FormField(ActionForm), min_entries=1)