[英]Key Error when trying to get header value
I am just trying to request a form value in flask. 我只是想在flask中请求一个表单值。 Something i have done many times before with no issue but I cannot figure out why I am getting this key error.
我之前做了很多次都没问题,但是我无法弄清楚为什么会出现这个关键错误。
I have tried many things like switching from a get request to a post request but nothing seems to be working. 我尝试了很多事情,例如从get请求切换到post请求,但似乎没有任何效果。
App Route: 应用程序路线:
@app.route('/Exercise', methods = ['GET'])
def exercise():
test = request.form["title"]
print(test)
if test != None:
info = select(["*"],"Exercises",["Name"],"And",[test])
print(info)
return render_template('exercise.html')
HTML: HTML:
<body>
<form>
<h2 class="head" id="name" name="title"></h2>
<p class="lead">Details</p>
</form>
</body>
<script type="text/javascript" src="static/js/exercise.js"></script>
</html>
Ok so thanks to PRMoureu I discovered I needed to make the called url contain the field I wanted to get with flask. 好的,所以感谢PRMoureu,我发现我需要使被调用的url包含我想通过flask获得的字段。 Example below: window.location.href = "/Exercise?title="+name;
下面的示例:window.location.href =“ / Exercise?title =” + name;
Thanks for the help much appreciated 感谢您的帮助,不胜感激
Since you're supporting the GET
method only try accessing your title
key like so: 由于您支持
GET
方法,因此请仅尝试访问您的title
键,如下所示:
request.args.get('title')
instead of test = request.form["title"]
as this is the way you would access the title
key in a POST
method. 而不是
test = request.form["title"]
因为这是您在POST
方法中访问title
键的方式。
Hopefully that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.