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.
Getting a 400 bad request error while submitting a flask form
Try using request.form.get("name_of_field")
instead of just request.form["name_of_field"]
.
When you use request.form["name_of_field"]
, flask assumes that they key will always be there. you canavoid this by using the other one, or the try
and except
trick.
Form sending error, Flask
As @Blubber points out, 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.
There are two other good ways to deal with your situation:
Use
request.form
's.get
method:if request.form.get('add', None) == "Like":
# Like happened
elif request.form.get('remove', None) == "Dislike":
# Dislike happenedUse the same
name
attribute for both submit elements:<input type="submit" name="action" value="Like">
<input type="submit" name="action" value="Dislike">
# and in your code
if request.form["action"] == "Like":
# etc.
Related Topics
Using Headers with the Python Requests Library's Get Method
Can Modules Have Properties the Same Way That Objects Can
Why Doesn't This Division Work in Python
Fastapi Runs API-Calls in Serial Instead of Parallel Fashion
Pip Freeze Creates Some Weird Path Instead of the Package Version
How to Get the Index of a Maximum Element in a Numpy Array Along One Axis
How to Split a Multi-Line String into Multiple Lines
How to Execute a Python Script from the Django Shell
How to Define a Function with Optional Arguments
What's an Efficient Way to Find If a Point Lies in the Convex Hull of a Point Cloud
Matplotlib (Equal Unit Length): with 'Equal' Aspect Ratio Z-Axis Is Not Equal to X- and Y-
Test If Executable Exists in Python
How to Save a Dictionary to a File
Replacing Numpy Elements If Condition Is Met
How to Generate All Possible Three Letter Strings
How to Increment Datetime by Custom Months in Python Without Using Library