Multiple instances of the same form field

后端 未结 1 2032
情深已故
情深已故 2021-02-10 00:44

I have invite form with two fields defined as person and email as follows:

class InviteForm(Form):
    person = TextField(\"person\", validators=[validators.Requ         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 01:00

    What you're looking for is FormField which lets you build a list of the WTF Fields (or groups even).

    Here's a simple example-- it'll render three string fields as a html list because that's the minimum required. If you want to add extras via javascript, then just adhere to the same naming scheme, in the case below, a fourth would have a name of person-3.

    from flask import Flask, render_template_string
    from flask.ext.wtf import Form
    from wtforms import FieldList, StringField
    
    app = Flask(__name__)
    app.secret_key = 'TEST'
    
    
    class TestForm(Form):
        person = FieldList(StringField('Person'), min_entries=3, max_entries=10)
        foo = StringField('Test')
    
    
    @app.route('/', methods=['POST', 'GET'])
    def example():
        form = TestForm()
        if form.validate_on_submit():
            print form.person.data
            ## [u'One', u'Two', u'Three']
    
        return render_template_string(
            """
                
    {{ form.hidden_tag() }} {{ form.person }}
    """, form=form) if __name__ == '__main__': app.run(debug=True)

    0 讨论(0)
提交回复
热议问题