码迷,mamicode.com
首页 > 其他好文 > 详细

Flask路由之重定向

时间:2019-11-30 13:37:20      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:示例   request   int   需要   定向   直接   get   point   pre   

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

 

 

Flask路由之重定向

标签:示例   request   int   需要   定向   直接   get   point   pre   

原文地址:https://www.cnblogs.com/z-qinfeng/p/11962040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!