gpt4 book ai didi

python - Flask 中的重定向实际上并没有重定向到所需的页面。

转载 作者:行者123 更新时间:2023-11-28 18:43:10 28 4
gpt4 key购买 nike

class XX(MethodView):
def get(self):
....

def post(self):
error = None
user = request.form['username']
password = request.form['password']

print user,password
if user == 'xxx' and password == 'xx':
session['logged_in'] = True
session['session_user'] = 'xx'
return redirect("control")

else:
errors = []
errors.append("Login error")
return render_template("login.html" , errors=errors)

#adding rule for control Blueprint
control.add_url_rule("/control",view_func= ControlView.as_view('control'))

代码片段检查特定的用户名和密码,并应重定向到特定页面。

当请求登录页面时,发送 GET 请求。上面的代码片段获取用户名和密码并添加到 session 字典。重定向方法导致 POST 请求和响应代码为 302 FOUND,然后是对所需的 GET 请求带有 200 OK 响应代码的页面。

但是重定向并没有发生,它仍然在同一个登录页面上。

重定向应该有什么参数?

redirect('control')

redirect(url_for('control'))

redirect(url_for('control.control'))

为什么使用 render_template('XX.html') 以 500 响应代码响应 ??

最佳答案

此代码是否位于名为“control”的蓝图中? 我猜是的,根据上面显示的代码。

当您使用蓝图时,您无法提前知道最终 URL(例如:/control),因为蓝图用户可以将基本 URL 映射到他们想要的任何地方。

假设这是一个蓝图,重定向到您的 View 的最佳方法是使用蓝图 URL 表示法:redirect(url_for('control.control'))

如果这不会在蓝图中使用,您应该能够执行类似的操作:redirect(url_for('control')) 只要添加到时这是您的 View 名称路线。

希望这是清楚的。

如果您在尝试上述两个示例时仍然遇到错误,请启用 Flask 调试 (app.config['DEBUG'] = True) 并重试。追溯应该更深入地解释正在发生的事情。

关于python - Flask 中的重定向实际上并没有重定向到所需的页面。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com