Flask App Using WTForms with SelectMultipleField

0 votes

Hi guys. I've been practicing my coding skills by implementing what I've learned and built an application using Flask. Now in the application, I'm using the WTForms for user input, which uses a SelectMultipleField in a form. My problem is that, I can't seem to make the app POST all the items in the field when selected. It only sends the first item selected, no matter how many fields the user selects.

The Flask documentation says this about the data sent from this field type, but I don't see this behavior:

The data on the SelectMultipleField is stored as a list of objects, each of which is checked and coerced from the form input.

Here's a complete, minimal Flask app that illustrates this:

#!/usr/bin/env python

from flask import Flask, render_template_string, request
from wtforms import Form, SelectMultipleField

application = app = Flask('wsgi')

class LanguageForm(Form):
    language = SelectMultipleField(u'Programming Language', choices=[('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')])

template_form = """
{% block content %}
<h1>Set Language</h1>

<form method="POST" action="/">
    <div>{{ form.language.label }} {{ form.language(rows=3, multiple=True) }}</div>
    <button type="submit" class="btn">Submit</button>    
</form>
{% endblock %}

"""

completed_template = """
{% block content %}
<h1>Language Selected</h1>

<div>{{ language }}</div>

{% endblock %}

"""

@app.route('/', methods=['GET', 'POST'])
def index():
    form = LanguageForm(request.form)

Aug 23, 2018 in Python by charlie_brown
• 7,720 points

edited Aug 23, 2018 by charlie_brown 19,038 views

1 answer to this question.

0 votes

Flask returns request.form as a werkzeug MultiDict object. This is kind of like a dictionary, only with traps for the unwary.

http://flask.pocoo.org/docs/api/#flask.requesthttp://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict

MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values, too, you have to use the list methods.

However, I think there's an easier way. Can you do me a favor and try replacing:

language =  request.form['language']

with

language =  form.language.data

and see if that's any different? WTForms should handle the MultiDict object and just return a list for you since you've bound form data to it.

answered Aug 23, 2018 by aryya
• 7,450 points

Related Questions In Python

0 votes
1 answer
+2 votes
4 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,520 points
119,642 views
0 votes
1 answer

Open multiple files using with open() in Python?

Just replace and with , and you're done: with open('a', 'w') as ...READ MORE

answered Oct 31, 2018 in Python by Priyaj
• 58,090 points
8,341 views
0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,080 points

edited Dec 14, 2018 by Anirudh 1,140 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
0 votes
1 answer

Need help with Tkinter window formatting using Python

Tkininter comes with the columnspan argument to span the labels ...READ MORE

answered Sep 7, 2018 in Python by aryya
• 7,450 points
629 views
0 votes
1 answer

Python using basicConfig method to log to console and file

I can't reproduce it on Python 3.3. ...READ MORE

answered Aug 14, 2018 in Python by aryya
• 7,450 points
873 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP