Q
Q
quges12018-03-14 16:44:24
Flask
quges1, 2018-03-14 16:44:24

How to add extra fields with Flask-wtf?

fT0Kj.jpg
I want the user to be able to add or remove fields on the website. I know how to implement this directly with js, but what HTML attributes should I use to have flask-wtf write information to form.options .data ?

forms.py

class OptionsForm(FlaskForm):
    option = StringField('options')

class CreatePollForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired()])
    options = FieldList(FormField(OptionsForm), min_entries=2)
    submit = SubmitField('Create')


Sample
<form action="{{ url_for('poll.new')}}" method="post" name="login">
    {{form.hidden_tag()}}
    <input type="text" name="title" />
    <input type="text" name="options" id="options_0" />
    {{ form.csrf_token }}
    <p><input type="submit" value="Sign In"></p>
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
quges1, 2018-03-15
@quges1

class CreatePollForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired()])
    options = FieldList(StringField())
    submit = SubmitField('Create')

<ul id="options">
        <li>
            <label for="options-0">Options-0</label>
            <input id="options-0" name="options-0" type="text" value="">
        </li>
        <li>
            <label for="options-1">
                Options-1
            </label>
            <input id="options-1" name="options-1" type="text" value="">
        </li>
        <li>
            <label for="options-2">Options-2</label>
            <input id="options-2" name="options-2" type="text" value="">
        </li>
   ...........
    </ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question