使用WTForms,我希望在整个表单中重复此块:
<div class="form-group input-group">
<select name="multiple[]" class="form-control">
<option value="">Default select</option>
<option value="">Option 1</option>
<option value="">Option 2</option>
</select>
<input type="text" name="multiple[]" class="form-control">
<span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+</button></span>
</div>
对于多个字段(具有相同的名称属性),我使用wtforms.fields.FieldList附件是否正确?
如何在最后添加span
元素?
答案 0 :(得分:2)
我能够使用另一个表单子类解决问题。
forms.py(WTForms):
class SubForm(Form):
key = SelectField("Type: ", coerce=int, choices=[(0, "Default select"), (1, "Option 1"), (2, "Option 2")], default=0)
value = StringField()
class ParentForm(Form):
...
multiple = FormField(SubForm, label="Multiple: ")
submit = SubmitField("Submit")
HTML模板(Jinja):
{{ form.multiple.label }}
<div class="form-group input-group">
{% for field in form.multiple %}
{{ field(class='form-control') }}
{% endfor %}
<span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+</button></span>
</div>