【发布时间】:2020-07-25 09:16:50
【问题描述】:
我在 index.html 中有一个下拉菜单,在 app.py 中有一个 Flask 函数,它应该根据下拉选择来做一些事情。但是选择值没有被发送到 Flask 函数。
在 index.html 中:
<form method="POST">
<select name = "text">
<option value="Iliad">Iliad</option>
<option value="Odyssey">Odyssey</option>
</select>
</form>
在 app.py 中:
@app.route("/", methods = ['POST', 'GET'])
def home():
selection = request.form.get('text')
if selection == 'Odyssey':
# do some stuff
if selection == 'Iliad':
# do some stuff
# some other code
return render_template("index.html")
当我加载页面时,“一些其他代码”位运行良好。但是,如果我随后从下拉菜单中进行选择,则“做一些事情”代码都不会运行,即它没有输入条件。
我也尝试在两个条件之前添加“if request.method == 'POST':”,但这似乎没有什么区别。
帮助?
【问题讨论】:
-
我在自己的项目中使用 request.form['text'] 并且有效,所以也许可以试试?不知道为什么会有所作为。
-
@nj1234 我试过了,但我收到此错误消息:“werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent an request that this server could not understand. KeyError: '文本'"
标签: python html flask drop-down-menu