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

試著忘記壹切 提交于 2019-11-26 13:27:14
zch

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.

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