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)
lpr()will needform = LPRForm(request.POST)or similar, otherwise you are always processing a new form with no user data submitted (the form forGETmethod).form=LPRForm()and the browser is submitting the user data back to the server. What's the purpose ofrequest.POST?request.POSTgets you aMultiDictin bottle and then you instantiate the form with it as a template for the form object attributes (after the post action the fields will be filled). I think from the Flask docs there is norequest.POST, and looks like it is not needed this way. Probablyrequest.formgives the same thing.