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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…