从 HTML 表单发布值并在 Flask 视图中访问它们 [英] Post values from an HTML form and access them in a Flask view
本文介绍了从 HTML 表单发布值并在 Flask 视图中访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个发布到 Flask 路由的 HTML 表单.但是,request.form
是空的.如果我尝试通过 id 访问其中一个值,则会收到 400 错误.如何从 HTML 表单发布值并在 Flask 中访问它们?
I have an HTML form that gets posted to a Flask route. However, request.form
is empty. If I try to access one of the values by id, I get a 400 error. How do I post values from an HTML form and access them in Flask?
<form method="POST">
<input id="my_input" type="text" value="{{ email }}">
<input id="my_submit" type="submit" value="Submit">
</form>
@app.route('/page', methods=['POST', 'GET'])
def get_page():
if request.method == 'POST':
print(request.form) # prints ImmutableMultiDict([])
print(request.form['my_input']) # raises 400 error
return render_template('page.html')
推荐答案
您的 input
没有 name
属性.这就是客户端将传递给服务器的内容.如果您访问未提交的表单键,Flask 将引发 400 错误.
Your input
doesn't have a name
attribute. That is what the client will pass along to the server. Flask will raise a 400 error if you access a form key that wasn't submitted.
<input name="my_input" id="my_input" type="text" value="{{ email }}">
这篇关于从 HTML 表单发布值并在 Flask 视图中访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文