Python wtforms SelectField始终返回无
因此,我正在使用FlaskForm和wtforms构建一个包含SelectField的web表单。当用户提交表单时,我可以毫无问题地读取StringFields,但是SelectField的值始终为None 我一直在寻找解决这个问题几个小时了,但既找不到答案,也找不到解释我的代码出了什么问题 我查看了以下线程,但没有成功:Python wtforms SelectField始终返回无,python,flask,flask-wtforms,wtforms,Python,Flask,Flask Wtforms,Wtforms,因此,我正在使用FlaskForm和wtforms构建一个包含SelectField的web表单。当用户提交表单时,我可以毫无问题地读取StringFields,但是SelectField的值始终为None 我一直在寻找解决这个问题几个小时了,但既找不到答案,也找不到解释我的代码出了什么问题 我查看了以下线程,但没有成功: 还有更多,我再也找不到了。我还通读了一些文章和例子 下面是一些重现问题的代码: from flask import Flask, url_for, redirect,
from flask import Flask, url_for, redirect, request, render_template_string
from flask_wtf import FlaskForm
from wtforms import SelectField, SubmitField
app = Flask(__name__)
app.config["SECRET_KEY"] = "very_secret"
# basic html template
my_html = """
<div class="content-section">
<form method="POST" action=".">
{{form.hidden_tag()}}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Task</legend>
<div class="form-group">
{{ form.select.label(class="form-control-label")}}
<select class="form-control" id="continent_selector">
{% for choice in form.select.choices %}
<option value="{{ choice[1] }}">{{ choice[0] }}</option>
{% endfor %}
</select>
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info")}}
</div>
</form>
</div>
"""
class TaskForm(FlaskForm):
tuple_list = [("Choice_0", 0), ("Choice_1", 1)]
select = SelectField("select", choices=tuple_list)
submit = SubmitField("Create Task")
@app.route("/", methods=["GET", "POST"])
def new_task():
form = TaskForm()
if request.method == "POST":
print("-------------FORM--------------")
print(form.data)
print("-------------END--------------")
return str(form.select.data)
return render_template_string(my_html, title="New Task", form=form)
if __name__ == "__main__":
app.run(debug=True)
如果有人能指出我做错了什么,那就太好了:)您的
select
标签需要一个名字。替换:
与:
给这位学者一票!
{'select': 'None', 'submit': True, 'csrf_token': 'some_hash'}