WTForms + Multiple Fields&追加跨度

时间:2014-06-06 13:56:49

标签: wtforms flask-wtforms

使用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>
  1. 对于多个字段(具有相同的名称属性),我使用wtforms.fields.FieldList附件是否正确?

  2. 如何在最后添加span元素?

1 个答案:

答案 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>