• 【Flask】 python学习第一章


    jinjia2 模板 python实现 flask 内置语言  参照Djago实现 

    设置模板文件夹

    设置模板语言 jinja2 

    demo6_template.html  ----> 从代码渲染值 

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    你有事情我帮忙  我住隔壁我姓王<br>
    {#取值#}
    {{ my_int}}
    {{ my_list}}
    </body>
    </html>
    demo6_template.html

    demo6_template.py    ------> render_templete("demo6_template.html", 传入渲染值 )

    from flask import Flask, request, render_template
    
    app = Flask(__name__)
    
    # 展示模板渲染
    @app.route("/")
    def demo1():
        my_str = "12345"
        my_int = 10
        my_list = ["a", "b", "c", "d"]
        return render_template('demo6_template.html',
                               my_str=my_str,
                               my_int=my_int,
                               my_list=my_list)
    
    
    if __name__ == '__main__':
        app.run(debug=True,port=2222)
    demo6_template.py

    过滤器 

    {{itheima | upper}}    

    from flask import Flask, request, render_template
    
    app = Flask(__name__)
    
    
    # 展示模板渲染
    @app.route("/")
    def demo1():
        my_str = "12345"
        my_int = 10
        my_list = ["a", "b", "c", "d"]
        my_dict = [
                    {"username":"白菜",
                    "price": 20
                    },
                    {
                       "username": "萝卜",
                       "price": 30
                    }
                ]
        return render_template('demo6_template.html',
                               my_str=my_str,
                               my_int=my_int,
                               my_list=my_list,
                               my_dict=my_dict
                               )
    
    
    # 自定义过滤器  本质上python函数
    # 装饰器 反转
    #                       过滤器名称
    # 方式一
    @app.template_filter("listreverse")
    def do_listreverse(li):
        temp = list(li)
        temp.reverse()
        return temp
    
    # 方式二 直接添加过滤器
    # app.add_template_filter(do_listreverse,"listreverse")
    
    
    if __name__ == '__main__':
        app.run(debug=True,port=2222)
    demo6.py
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    你有事情我帮忙  我住隔壁我姓王<br>
    {#取值#}
    {{ my_int}}
    {{ my_list}}
    {#大写#}
    {{ 'itheima' | upper}}
    {#小写#}
    {{ 'ITHEIMA' | lower }}
    {#反转#}
    {{ 'THEIMA' | reverse }}
    {#反转小写#}
    {{ 'ITHEIMA' | reverse | lower }}
    
    {#默认html字符串按照原样子显示 如果标识safe 就会按照html语言格式 进行执行  相反 escape#}
    {{ my_list | safe }}
    
    {#传参数 求和 #}
    {{ my_dict | sum(attribute='price') }}
    
    {#列表反转#}
    <br>
    {{ my_list | listreverse }}
    <br>
    {{ my_list }}
    
    </body>
    </html>
    demo6.html

    循环遍历 

    {%  for item in my_list  if my_list.id  != 5 %}

      {% if loop.index == 1%}

        <li>item.value<li>

      {%  endif %}

    {% endfor %}

    for.py
    for.html

    未完待续

    代码复用

    macro 宏   

     #  定义所有宏文件  

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form>
        <label>用户名:</label><input type="text" name="username"><br/>
        <label>身份证号:</label><input type="text" name="idcard"><br/>
        <label>密码:</label><input type="password" name="password"><br/>
        <label>确认密码:</label><input type="password" name="password2"><br/>
        <input type="submit" value="注册">
    </form>
    
    {% macro input(label='',type='',value = '',name='') %}
        <label>{{ label }}</label><input type = "{{ type }}" name = "{{ name }}">
    {% endmacro %}
    
    <form>
        {{ input('用户名',name = 'username') }}
    </form>
    </body>
    </html>
    macro.html
    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("demo1_template.html")
    
    
    if __name__ == '__main__':
        app.run()
    macro.py

    继承

    demo:

    {%block contentblock%} 

       填充内容 

    {%endblock%}

    extends:

    {% extends "demo.html"%}

    {%block contentblock%}

      {{super()}} 继承父类内容

         子类内容
    {%endblock%}

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def index():
        return render_template("demo1_template.html")
    
    
    @app.route("/demo1")
    def demo1():
        return render_template("demo2_template.html")
    
    
    @app.route("/demo")
    def demo():
        return render_template("demo.html")
    
    
    if __name__ == '__main__':
        app.run()
    extend.py
    {% extends 'demo2_template.html'%}
    
    {% block contentblock %}
        {{ super()  }}<br/>
        我是子类中间<br/>
    {% endblock %}
    extends.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    我的顶部<br/>
    {% block contentblock %}
        我是父类的中间<br/>
    {% endblock %}
    我是底部<br/>
    </body>
    </html>
    base.html

    包含:

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def index():
        return render_template("demo1_template.html")
    
    #
    @app.route("/demo1")
    def demo1():
        return render_template("demo2_template.html")
    
    # 继承
    @app.route("/demo")
    def demo():
        return render_template("demo.html")
    
    # 包含
    @app.route("/demo2")
    def demo2():
        return render_template("demo3_template.html")
    
    
    if __name__ == '__main__':
        app.run()
    include.py
    <h1>哈哈哈哈哈哈</h1>
    include.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
     {% include "include.html" %}
     {% include "include.html" %}
     {% include "include.html" %}
     {% include "include.html" %}
    </body>
    </html>
    demo3_template.html

    未完待续

  • 相关阅读:
    dubbo的超时机制
    今天又遇到之前的问题,后端返回数据long到前端失真
    如何在一台机子上配置两个github
    当sum函数返回null时处理
    Linux中zookeeper安装
    Linux常用指令
    sql执行顺序
    固定时间刷新某个固定值 java
    docker基础
    python之CSS
  • 原文地址:https://www.cnblogs.com/oscarli/p/12064028.html
Copyright © 2020-2023  润新知