重定向 和 反向引用
1. 重定向-redirect: 有两次响应
(1) 302状态码 + Location
(2) 返回location请求地址内容
2. 反向引用-url_for
路由中定义endpoint 参数,使用 url_for(函数名)进行反向引用
import jsonfrom flask import Flask, url_for, redirectimport settingsapp = Flask(__name__)
app.config.from_object(settings) # 加载配置文件# 1. 路由中定义endpoint 参数,使用 url_for(函数名)进行反向引用
@app.route("/index", endpoint="index_data")
def index_data():return "首页" # 首页@app.route("/test")
def test():# url = url_for("index") # 反向解析,配合endpoint使用:根据函数名找路径# print(url) # /index# return redirect("/index") # 重定向: 直接使用路由return redirect(url_for("index_data")) # 2. 重定向: 使用反向引用
if __name__ == '__main__':app.run(port=8080)