Flask007_页面重定向

发布时间 2023-05-26 10:27:36作者: jason2018
  • 页面重定向:浏览器从一个页面自动跳转到另一个页面。
  • 例如,用户访问一个需要权限的页面,但是该用户当前没有登录,因此重定向到登录页面。

永久性重定向

  • HTTP 的状态码是301。

暂时性重定向

  • HTTP 的状态码是302。
1 @app.route('/profile')
2 def profile():
3     # 通过字符串查询方式传递 name 参数
4     name = request.args.get('name')
5     if not name:
6         return redirect('/login', code=302)
7     else:
8         return f'这是{name}的个人中心'
9     return '这是个人中心'
  • 在访问/profile 时,如果没有通过查询字符串的方式传递 name 参数,那么就会被重定向到/login。
  • 在访问/profile?name=jason ,不重定向。