列表在 Flask/WTForms 中转换为下拉列表
List converted to Dropdown in Flask/WTForms
基本上我在 Python 中有一个列表,我想以编程方式调用并使用 WTForms 从该列表创建一个下拉表单到 HTML 文档中。尝试在 WTForms 中使用 SelectField 方法时,我无法弄清楚我在这里遗漏了什么。该列表在 "updatef" 内。
我收到错误:ValueError:需要超过 1 个值才能解包
当尝试 运行 这个时。
class ReusableForm(Form): # Define the form fields and validation parameters
updates = SelectField(u'Update Frequency', choices = updatef, validators = [validators.required()])
@app.route("/editor", methods=['GET', 'POST'])
def hello():
form = ReusableForm(request.form) # calls on form
if request.method == 'POST':
updates = request.form['updates']
HTML
<form id="main" action="" method="post" role="form" spellcheck="true">
<p> {{ form.updates }} </p>
choices
必须是 2 个值元组的列表,例如 [(1,'Daily'),(2,'Weekly')]
- 您的错误似乎表明您可能只有一个值列表。
基本上我在 Python 中有一个列表,我想以编程方式调用并使用 WTForms 从该列表创建一个下拉表单到 HTML 文档中。尝试在 WTForms 中使用 SelectField 方法时,我无法弄清楚我在这里遗漏了什么。该列表在 "updatef" 内。
我收到错误:ValueError:需要超过 1 个值才能解包 当尝试 运行 这个时。
class ReusableForm(Form): # Define the form fields and validation parameters
updates = SelectField(u'Update Frequency', choices = updatef, validators = [validators.required()])
@app.route("/editor", methods=['GET', 'POST'])
def hello():
form = ReusableForm(request.form) # calls on form
if request.method == 'POST':
updates = request.form['updates']
HTML
<form id="main" action="" method="post" role="form" spellcheck="true">
<p> {{ form.updates }} </p>
choices
必须是 2 个值元组的列表,例如 [(1,'Daily'),(2,'Weekly')]
- 您的错误似乎表明您可能只有一个值列表。