flask 的管理模块的功能add_template_global、send_from_directory

add_template_global方法

全局模板函数

add_template_global  装饰器直接将函数注册为模板全局函数.

add_template_global 这个方式是自定义的全局函数和django的inclusion_tag类似,这个是为了让你的 前端界面可以通过这个url找到需要的元素   其中第一个参数是自定义的全局函数 ,第二个参数是自定义的全局函数的名字


app.add_template_global(function, name)传入函数对象和自定义名称注册自定义模板函数, 第一个参数是你的方法  第二个参数是你方法的名字(名字记得加上字符串)

 

app.add_template_global(UrlManage.buildUrl, "buildUrl")
app.add_template_global(UrlManage.buildStaticUrl, "buildStaticUrl")

 

 

 

 

 

send_from_directory

这是 接口返回真实的文件,就是帮你找到你的所需要的文件

from flask import Blueprint,send_from_directory
from application import app
route_static = Blueprint('static', __name__)


@route_static.route("/<path:filename>")
def index(filename):
    #需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
    return send_from_directory(app.root_path+"/web/static/", filename)

 

原文地址:https://www.cnblogs.com/zhaoyunlong/p/10337956.html