Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

python - Converting Flask form data to JSON only gets first value

I want to take input from an HTML form and give the output in JSON format. When multiple values are selected they are not converted into JSON arrays, only the first value is used.

@app.route('/form')
def show_form():
    return render_template('form.html')

@app.route("/result", methods=['POST'])
def show_result():
    result = request.form
    return render_template('result.html', result=result)

form.html:

<form method=POST>
   <input name=server>
   <select name=owners multiple>
       <option value="thor">thor</option>
       <option value="loki">loki</option>
       <option value="flash">flash</option>
       <option value="batman">batman</option>
   </select>
   <input type=submit>
</form>

result.html:

{{ result|tojson }}

When multiple values for owner are selected, "thor" and "flash", the output shows only one value:

{"server": "app-srv", "owners": "thor"}

I expect owners to be a list:

{"server": "app-srv", "owners": ["thor", "flash"]}

How do I display the form as JSON without losing list values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

request.form is a MultiDict. Iterating over a multidict only returns the first value for each key. To get a dictionary with lists of values, use to_dict(flat=False).

result = request.form.to_dict(flat=False)

All values will be lists, even if there's only one item, for consistency. If you want to flatten single-value items, you need to process the data manually. Use iterlists with a dict comprehension.

result = {
    key: value[0] if len(value) == 1 else value
    for key, value in request.form.iterlists()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...