Flask框架提供了请求重定向功能,只需要使用 redirect_to即可, 示例代码如下:

from  flask import Flask, render_template, request, redirect, session

app = Flask(__name__)
app.secret_key = 'flask'
app.debug = True
"""
    redirect_to: 会将请求index 重定向到index2
"""
@app.route('/index',methods=['GET'],endpoint='r1',redirect_to='/index2')
def index():
    print('老首页')
    return "老首页"


@app.route('/index2',methods =['GET','POST'])
def index2():
    print('新首页')
    return "新首页"

if __name__ == '__main__':
    app.run()

注意: 浏览器请求到index路由时, flask框架帮我直接转发到index2路由, 根本就不会进入内index方法内部

 

127.0.0.1 - - [30/Nov/2019 12:36:57] "GET /index2 HTTP/1.1" 200 -
新首页




print('老首页')根本就没有执行

 

相关文章:

  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
  • 2021-07-12
  • 2021-07-02
  • 2021-09-13
  • 2021-12-02
猜你喜欢
  • 2021-05-15
  • 2021-11-26
  • 2021-06-14
  • 2021-08-03
  • 2021-06-18
  • 2022-01-26
  • 2022-12-23
相关资源
相似解决方案