简体   繁体   中英

FlaskForm wtforms multiple input fields single submit

Using FlaskForm and SelectMultipleField, I'm creating a table of choices allowing for multiple selection to be ranked: 1, 2, 3, ... .

In order to place the choices in a grid pattern on a table, I made each row it's own instance of SelectMultipleField.

The submit button is only returning the values of the first instance of SelectMultipleField (dogs).

How can I get the submit button to return values in all instances of SelectMultipleField?

Here's the class in my forms module:

class LPRForm(FlaskForm):
    party = ['Dogs', 'Cats', 'Rabbits', 'Birds']
    dog = [('', 'D. Duke'), ('', 'R. Rusty'), \
             ('', 'T. Tucker'), ('', 'R. Roger')]
    cat = [('', 'S. Shadow'), ('', 'M. Misty'), \
             ('', 'P. Patch'), ('', 'P. Paws')]
    rabbit = [('', ''), ('', 'C. Clover'), ('', ''), ('', '')]
    bird = [('', 'P. Pikachu'), ('', 'S. Starburst'), \
              ('', ''), ('', 'F. Flighty')]

    vote_dog = SelectMultipleField('District', choices=dog, 
                        option_widget=widgets.TextInput() )
    vote_cat = SelectMultipleField('District', choices=cat, 
                        option_widget=widgets.TextInput() )
    vote_rabbit = SelectMultipleField('District', choices=rabbit, 
                        option_widget=widgets.TextInput() )
    vote_bird = SelectMultipleField('District', choices=bird, 
                        option_widget=widgets.TextInput() )

    submit = SubmitField('Cast Ballot')

Here's the relevant protion of the html file:

<table style="width:100%" align="center">
    <tr>
    <td>  </td>
    {% for vote in form.vote_dog %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_cat %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_rabbit %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>

    <tr>
    <td>  </td>
    {% for vote in form.vote_bird %}
        {% if vote.label != '': %}
            <td>{{ vote(size="2") }}</td> <td>{{ vote.label }}</td>

        {% endif %}
    {% endfor %}
    </tr>
</table>
</td>
</tr>

{{ form.submit }}

And the view module:

@app.route('/lpr', methods=['GET', 'POST'])
def lpr():
    form = LPRForm() 
    return render_template('lpr.html', title='Home', form=form)

I was able to get this working by using FormField. However, the documentation gives a default separator as - . Additionally, I explicitly identified separator='-' , but only when I used . as a separator, when calling the class, did it work properly, ... even with separator='-' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM