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)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM