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

29,682

Solution 1

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

you have typed Id instead of name

Solution 2

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

Solution 3

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.

Share:
29,682

Related videos on Youtube

 younus
Author by

younus

Updated on December 06, 2020

Comments

  •  younus
    younus over 3 years

    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