zoukankan      html  css  js  c++  java
  • flask之模板html中调用python函数方法

    一:html里面可以调用python写的函数

    add_template_global(调用函数的引用,"调用函数的名字")

    from common.libs.UrlManager import UrlManager
    app.add_template_global(UrlManager.buildStaticUrl,"buildStaticUrl")
    app.add_template_global(UrlManager.buildUrl,"buildUrl")
    app.add_template_global(UrlManager.buildImageUrl,"buildImageUrl")

    作用:这个是为了让前端界面可以通过这个url找到需要的元素

     <img src="{{ buildStaticUrl('/images/common/qrcode.jpg') }}" width="200px">
    # -*- coding: utf-8 -*-
    import time
    from application import app
    
    class UrlManager(object):
        def __init__(self):
            pass
    
        @staticmethod
        def buildUrl( path ):
            return path
    
        @staticmethod
        def buildStaticUrl(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 else release_version
            path =  "/static" + path + "?ver=" + ver
            # 对path进行拼接,路径是静态文件里面的图片和css,js等资源
            return UrlManager.buildUrl( path )
    
        @staticmethod
        def buildImageUrl( path ):
            app_config = app.config['APP']
            url = app_config['domain'] + app.config['UPLOAD']['prefix_url'] + path
            return url

    设计蓝图

    from flask import Blueprint,send_from_directory
    from application import app
    
    route_static = Blueprint("static",__name__)
    @route_static.route("/<path:filename>")
    def index(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(文件所在的文件夹,文件名)的作用:就是帮你找到你的所需要的文件,传入的参数是路径和文件名

     <img src="{{ buildStaticUrl('/images/common/qrcode.jpg') }}" width="200px">

    过程:html里面调用了 buildStaticUrl(包含路径),函数的返回值是一个path,给src赋值为这个path,然后就发送了请求,按照这个路径进行寻找,然后触发了route_static这个蓝图,进入index函数,通过send_from_directory方法,找到文件真实的路径,进行展示







    # TODO
  • 相关阅读:
    flutter 使用keyboard_actions 关闭ios键盘
    Android Studio androidx 包冲突解决方法
    flutter 卡在Running Gradle task 'assembleDebug'...
    SpringBoot Controller接收参数的几种常用方式
    RN 0.6以后react-navigation 导航报错null is not an object (evaluating '_RNGestureHandlerModule.default.Direction')
    flutter 调用摄像头和照片
    RN 真机roload
    iOS Undefined symbols for architecture armv7:
    android 签名生成和签名验证
    Keil MDK中Image~~RW_IRAM1~~ZI~~Limit(~表示$)
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12634149.html
Copyright © 2011-2022 走看看