一:html里面可以调用python写的函数
add_template_global(调用函数的引用,"调用函数的名字")
from common.libs.UrlManager importUrlManager
app.add_template_global(UrlManager.buildStaticUrl,"buildStaticUrl")
app.add_template_global(UrlManager.buildUrl,"buildUrl")
app.add_template_global(UrlManager.buildImageUrl,"buildImageUrl")
作用:这个是为了让前端界面可以通过这个url找到需要的元素
#-*- coding: utf-8 -*-
importtimefrom application importappclassUrlManager(object):def __init__(self):pass@staticmethoddefbuildUrl( path ):returnpath
@staticmethoddefbuildStaticUrl(path):
release_version= app.config.get( 'RELEASE_VERSION')print("这是什么玩意:",release_version) #访问login.html一共打印了10次None,源模板7次,本身打印3次
ver = "%s"%( int( time.time())) if not release_version elserelease_version
path= "/static" + path + "?ver=" +ver#对path进行拼接,路径是静态文件里面的图片和css,js等资源
returnUrlManager.buildUrl( path )
@staticmethoddefbuildImageUrl( path ):
app_config= app.config['APP']
url= app_config['domain'] + app.config['UPLOAD']['prefix_url'] +pathreturn url
设计蓝图
from flask importBlueprint,send_from_directoryfrom application importapp
route_static= Blueprint("static",__name__)
@route_static.route("/")defindex(filename):#因为static的位置也改变了,因此使用路由的方式来访问我们静态文件
return send_from_directory(app.root_path+"/web/static/",filename)
注册蓝图
app.register_blueprint(route_static,url_prefix="/static")
请求成功后,控制台输出
111.18.99.43 - - [04/Apr/2020 20:47:50] "GET /static/css/style.css?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:52] "GET /static/plugins/jquery-2.1.1.js?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:52] "GET /static/images/common/mini_qrcode.jpg?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:53] "GET /static/bootstrap/bootstrap.min.js?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:53] "GET /static/plugins/layer/layer.js?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:53] "GET /static/images/common/qrcode.jpg?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:55] "GET /static/js/common.js?ver=1586004470 HTTP/1.1" 200 -
111.18.99.43 - - [04/Apr/2020 20:47:55] "GET /static/js/user/login.js?ver=1586004470 HTTP/1.1" 200 -
send_from_direcorty(文件所在的文件夹,文件名)的作用:就是帮你找到你的所需要的文件,传入的参数是路径和文件名
过程:html里面调用了 buildStaticUrl(包含路径),函数的返回值是一个path,给src赋值为这个path,然后就发送了请求,按照这个路径进行寻找,然后触发了route_static这个蓝图,进入index函数,通过send_from_directory方法,找到文件真实的路径,进行展示
# TODO