简体   繁体   English

尝试获取标头值时出现关键错误

[英]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.

相关问题 尝试通过动态键获取对象值时出现流类型错误 - Flowtype error when trying to get object value via dynamic key 尝试获取个人资料数据时,API密钥无效错误 - API key not valid error when trying to get profile data 当我尝试使用json获取API返回值时出错 - Error when I trying to get API return value using json 尝试从函数中获取值时发生错误 - error when trying to get value from function in react 尝试通过不存在的键“立即”向JavaScript关联数组添加新值 - Adding new value to javascript associative array “on the fly” when trying to get value by not existing key 尝试从Javascript中的键获取对象值 - Trying to get an object value in from a key in Javascript 尝试从 React.useEffect 中的 localStorage 获取键/值对时,无限循环 + 错误 output - Infinite loop + wrong output when trying to get key/value pair from localStorage in React.useEffect 试图在具有特定日期值的第一个元素上获取 header - Trying to get header over the first element with the value of specific date puppeteer 错误未知键:“”尝试按空格键时 - puppeteer Error Unknown key: " " when trying to press space key 如何将键/值参数附加到HTTP GET请求的标头? - How to attach key/value parameters to the header of a HTTP GET request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM