Flask App将WTForms与SelectMultipleField一起使用 [英] Flask App Using WTForms with SelectMultipleField
问题描述
我有一个Flask应用程序,该应用程序使用WTForms进行用户输入.它使用SelectMultipleField
形式.选择后,我似乎无法让该应用发布该字段中的所有项目;它仅发送选择的第一个项目,而不管用户选择了多少个项目.
I have a Flask application that uses WTForms for user input. It uses a SelectMultipleField
in a form. I can't seem to get the app to POST all items in the field when selected; it only sends the first item selected regardless of how many the user selects.
Flask文档谈到了从该字段类型发送的数据,但是我没有看到此行为:
The Flask documentation says this about the data sent from this field type, but I don't see this behavior:
SelectMultipleField上的数据存储为对象列表, 每一项都从表单输入中进行检查和强制.
The data on the SelectMultipleField is stored as a list of objects, each of which is checked and coerced from the form input.
这是一个完整的,最小的Flask应用程序,它说明了这一点:
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)
if request.method == 'POST' and form.validate():
print "POST request and form is valid"
language = request.form['language']
print "languages in wsgi.py: %s" % request.form['language']
return render_template_string(completed_template, language=language)
else:
return render_template_string(template_form, form=form)
if __name__ == '__main__':
app.run(debug=True)
推荐答案
Flask作为werkzeug MultiDict对象返回request.form.这有点像一本字典,只为那些粗心的人设陷阱.
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.request http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict
http://flask.pocoo.org/docs/api/#flask.request http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict
MultiDict实现所有标准的字典方法.在内部,它将键的所有值保存为列表,但是标准dict访问方法将仅返回键的第一个值.如果您也想访问其他值,则必须使用列表方法.
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']
使用
language = form.language.data
看看那有什么不同吗? WTForms应该处理MultiDict对象,并因为将表单数据绑定到该对象而只为您返回一个列表.
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.
这篇关于Flask App将WTForms与SelectMultipleField一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!