简体   繁体   中英

Key Error when trying to get header value

I am just trying to request a form value in 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.

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:

<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. Example below: 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:

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.

Hopefully that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM