• 模板


    模板

    from flask import Flask,render_template,redirect,request,jsonify,make_response,Markup
    app = Flask(__name__)
    
    @app.template_global()
    def sbbbbbbb(a1, a2):
        """
        每个模板中可以调用的函数
        :param a1:
        :param a2:
        :return:
        """
        return a1 + a2
    
    
    def gen_input(value):
        # return "<input value='%s'/>" %value
        return Markup("<input value='%s'/>" %value)
    
    @app.route('/x1',methods=['GET','POST'])
    def index():
        context = {
            'k1':123,
            'k2': [11,22,33],
            'k3':{'name':'oldboy','age':84},
            'k4': lambda x: x+1,
            'k5': gen_input, # 当前模板才能调用的函数
        }
    
        return render_template('index.html',**context)
    
    
    @app.route('/x2',methods=['GET','POST'])
    def order():
        context = {
            'k1':123,
            'k2': [11,22,33],
        }
    
        return render_template('order.html',**context)
    
    
    
    
    if __name__ == '__main__':
        app.run()

    index.html

    {% extends 'layout.html'%}
    
    {% block content %}
        <h1>{{k1}}</h1>
        <h1>{{k2.0}}  {{k2[0]}} </h1>
        <h1>{{k3.name}}  {{k3['name']}}  {{k3.get('name',888)}}</h1>
        <h1>{{k4(66)}}</h1>
        <h1>{{k5(99)}}</h1>
        <h1>{{sbbbbbbb(1,2)}}</h1>
    {% endblock%}

    order.html

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
        <h1>{{sbbbbbbb(1,2)}}</h1>
    </body>
    </html>

    layout.html

    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
        <div >头部</div>
        <div>
            {% block content %} {% endblock %}
        </div>
        <div >底部</div>
    </body>
    </html>
    天道酬勤 循序渐进 技压群雄
  • 相关阅读:
    ubuntu 12.04下 eclipse的安装
    ubuntu 12.04下 ns3的下载 安装
    win10下装上virtualbox 以及在virtualbox上装上 ubuntu 12.04
    Java并发,synchronized锁住的内容
    JVM深入理解
    华为面试流程
    eclipse将项目打包成jar在linux中运行
    1.整数反转
    静态方法
    java中生成任意之间数的随机数
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/14859463.html
Copyright © 2020-2023  润新知