I implemented WTForm's FieldList
with SelectField
's to render multiple select choices based on data in a JSON file (as stated in this answer). It displays the select choices fine, but when I try to pass all the values that the user selected via a SubmitField()
form, the request only contains the value from the first one.
Question: How can I access ALL the selected values from my multiple SelectFields from my route?
Interesting Note:
- I did this same
FieldList
approach with multipleStringFields
and get each of the values as expected in the request. (Why would this be different forSelectFields
?)
- Here
Ford
,Associate
, andBucks
are the values from the select fields.
The request response: (from request.form
):
- Notice how only
Ford
is shown. Why not alsoAssociate
andBucks
? - Notice how all the text fields show up correctly
My forms (forms.py
):
class SelectForm(FlaskForm):
select = SelectField(choices=[]) #TODO testing without placeholder
class SelectFormList(FlaskForm):
"""A form that supports multiple select forms"""
select_entries = FieldList(FormField(SelectForm))
My template (home.html
):
<form id="metadata_home_fom" action="{{ url_for('save_metadata') }}" method="POST">
{% for select_form in select_metadata_form_list.select_entries %}
{{ select_form.hidden_tag() }}
{{ select_form.select.label }}: {{ select_form.select }}
{% endfor %}
{% for text_form in text_metadata_form.text_fields %}
{{ text_form.hidden_tag() }}
{{ text_form.text }}
{% endfor %}
</form>
My view (routes.py
):
@app.route('/home', methods=['POST', 'GET'])
def home():
select_metadata_form_list = SelectFormList()
select_metadata_form_list.select_entries = get_select_entries()
context = {"text_metadata_form": text_metadata_form,
"select_metadata_form_list": select_metadata_form_list}
return render_template('configuration.html', **context)