[英]Error with POST request in Flask
My question is very similar to this but unfortunately the solution does not work for me. 我的问题是非常相似, 这可惜的解决方案并不为我工作。
I have created a web app and there is a 'Login as User' button which should redirect the user to a login form. 我创建了一个Web应用程序,有一个“用户身份登录”按钮应该将用户重定向到一个登录表单。
The app.py file looks like: 该app.py文件看起来像:
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form['action'] == 'Login as Guest':
create_user(request, session)
elif request.form['action'] == 'Login as User':
return redirect(url_for('login'), code=307)
elif request.form['action'] == 'Delete User':
delete_user(request, session)
else:
abort("invalid form")
return redirect(url_for('start'))
return render_template('index.html', username=session.get('username'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
return render_template('login.html', username=session.get('username'))
The login.html form is the following: 该login.html的格式如下:
<form class="form" action="" method="POST">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input class="btn btn-lg btn-success" name="login" type="submit" value="Login">
</form>
And the index.html form looks like: 而index.html的形式如下:
<form class="form" action="" method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="submit" name="action" class="btn btn-lg btn-success {% if username == None %}disabled{% endif %}" value="Delete User">
<input type="submit" name="action" class="btn btn-lg btn-success {% if username %}disabled{% endif %}" value="Login as Guest">
<input type="submit" name="action" class="btn btn-lg btn-success {% if username %}disabled{% endif %}" value="Login as User">
</form>
Even though I added code = 307
in the return redirect(url_for('login'), code=307)
call and the request method is POST, when I click on the Login as User button I get the following error: 即使我添加
code = 307
在return redirect(url_for('login'), code=307)
呼叫,请求方法是POST,当我登录点击为用户按钮,我得到以下错误:
Bad Request
错误的请求
The browser (or proxy) sent a request that this server could not understand.
浏览器(或代理)发送该服务器无法理解的请求。
click on the "Login as User" -> go to index endpoint -> redirect to login endpoint. 单击“以用户身份登录”->转到索引端点->重定向到登录端点。
the problem is when redirect to login endpoint, you can't get your username via request.form['username']
, nothing is in your form yet, thus you get the error. 问题是当重定向到登录端点时,您无法通过
request.form['username']
获取用户request.form['username']
,但表单中没有任何内容,因此会出现错误。
There are still some problems in your example: 您的示例中仍然存在一些问题:
I'll give you an simple example and hopt it helps. 我会给你一个简单的例子,希望对您有帮助。
@app.route('/', methods=['GET', 'POST'])
def index():
...
return redirect(url_for('login')) # no need for code 307
...
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == 'realusername' and password == 'realpasswd':
redirect(url_for('success_login'))
return render_template('login.html')
@app.route('/admin')
def success_login():
return "Login success!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.