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('老首页')根本就没有执行