What is the cause of the Bad Request Error when submitting form in Flask application? What is the cause of the Bad Request Error when submitting form in Flask application? python python

What is the cause of the Bad Request Error when submitting form in Flask application?


The solution was simple and uncovered in the comments. As addressed in this question, Form sending error, Flask, and pointed out by Sean Vieira,

...the issue is that Flask raises an HTTP error when it fails to find a key in the args and form dictionaries. What Flask assumes by default is that if you are asking for a particular key and it's not there then something got left out of the request and the entire request is invalid.

In other words, if only one form element that you request in Python cannot be found in HTML, then the POST request is not valid and the error appears, in my case without any irregularities in the traceback. For me, it was a lack of consistency with spelling: in the HTML, I labeled various form inputs

<input name="question1_field" placeholder="question one">

while in Python, when there was a POST called, I grab a nonexistent form with

request.form['question1']

whereas, to be consistent with my HTML form names, it needed to be

request.form['question1_field']

I hope this helps.