Flask路由之重定向

拟墨画扇 提交于 2019-12-06 04:34:21

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

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!