我有以下路线从提交的表单中获取值
@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')
我遇到的问题是我收到Bad Request The browser (or proxy) sent a request that this server could not understand.
我已经检查过我是否正确地从表单中获取了值,并且所有表单都包含其中的内容,但它似乎不起作用。
以下是呈现视图的模板
<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>
这是表单所在的视图类
@app.route('/signup')
def login():
return render_template("signup.html")
答案 0 :(得分:1)
99%的时间,此错误是您在request.form字典中请求不存在的密钥导致的密钥错误。要调试它,请运行
print(request.form)
并确保您请求的每个密钥都出现在字典中。这个错误特别令人沮丧(看似神秘),因为它不会触发你通常在Flask中获得的通常回溯。
查看以下问题:
What is the cause of the Bad Request Error .. ?
Form sending error, Flask