Bad request error flask with POST request

2024/10/6 13:19:58

I have the following route that is getting values from a submitted form

@app.route('/authenticate', methods=["POST"])
def authenticate():username = request.form['username']print(username, file = sys.stderr)password = request.form['password']email = request.form['email']models.User.create_user(email, password, username)return render_template('signup.html')

The problem I'm running into is that I'm getting a Bad Request The browser (or proxy) sent a request that this server could not understand.I have checked that I am correctly getting the values from the form, and that all the forms have content in them, but It's not seeming to work.

Here is the template that renders the view

<form action ="/authenticate" method="POST" id="signup"><fieldset class="form-group"><label for="InputUsername"> Username </label><input type="text" class="form-control" name="username" id="InputUsername" placeholder="Enter username"></fieldset><fieldset class="form-group"><label for="exampleInputEmail1">Email address</label><input type="email" class="form-control" name="email"id="exampleInputEmail1" placeholder="Enter email"><small class="text-muted">We'll never share your email with anyone else.</small></fieldset><fieldset class="form-group"><label for="exampleInputPassword1">Password</label><input type="password" class="form-control" name="password "id="exampleInputPassword1" placeholder="Password"></fieldset></form>

and here is my view class that the forms are on

@app.route('/signup')
def login():return render_template("signup.html")
Answer

99% of the time, this error is a key error caused by your requesting a key in the request.form dictionary that does not exist. To debug it, run

print(request.form)

and make sure that every key you request is present in the dictionary. This error is particularly frustrating (and seemingly mysterious) because it doesn't trigger the usual traceback you normally get in Flask.

Check out these questions:

What is the cause of the Bad Request Error .. ?
Form sending error, Flask

https://en.xdnf.cn/q/118947.html

Related Q&A

How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

Im wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity…

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…