werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'id'

假装没事ソ 提交于 2021-01-03 06:40:12

问题


html page

{%block title%}Login page{% endblock %}

{%block content%}
<form action = '#' method="post">
   <p>creds:</p>
   <p><input type="number"  placeholder="id"  Id="id" /></p>
   <p><input type="text"  placeholder="nm"  name="nm" /></p>
   <p><input type="submit" value="submit" /></p>
</form>
{%endblock%}

app code

@app.route("/")
def home():
    return render_template("login.html")

@app.route("/",methods = ["POST","GET"])
def post():
    if request.method == "POST":
        user = request.form['nm']
        id = request.form['id']
        sql = ('''INSERT INTO abc
                (id, name) VALUES (?, ?)
                ''')
        val = (id,user)
        cur.execute (sql, val)
    return 'Ok'

i tried using return.form.get('id') but its returning null

Can anyone please help me on this

Thanks


回答1:


<p><input type="number"  placeholder="id"  name="id" /></p>

you have typed Id instead of name




回答2:


When you use request.form["something"] you assume that this something always be part of your request, I recommend you to use request.form.get("something", False) to avoid that error. I hope this will solve your doubts.




回答3:


I had exactly the same problem, but mine was with a "SelectField" , the update fields would be "None" and to avoid that I just added option value None as shown below:

<option value="None">None</option>

This fixed my problem



来源:https://stackoverflow.com/questions/60599763/werkzeug-exceptions-badrequestkeyerror-400-bad-request-the-browser-or-proxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!