zoukankan      html  css  js  c++  java
  • Flask 页面缓存逻辑,jinja2 过滤器,测试器

    回调接入点-页面缓存逻辑

    from flask import Flask,request,render_template
    from werkzeug.contrib.cache import SimpleCache
    app = Flask(__name__)
    
    CACHE_TIMEOUT = 300
    cache = SimpleCache()
    cache.timeout = CACHE_TIMEOUT
    
    @app.before_request
    def return_cached():
        if not request.values:
            response = cache.get(request.path)
            if response:
                print('从网页获取了cache')
                return response
        print('将会加载网页')
    
    @app.after_request
    def cache_response(response):
        if not request.values:
            cache.set(request.path,response,CACHE_TIMEOUT)
        return response
    
    @app.route('/get_index')
    def index():
        return render_template('index.html')

     使用过滤器

     字符串过滤器

    {# 当变量未定义时,显示默认字符串,可以缩写为d #}
    <p>{{ name | default('No name', true) }}</p>
     
    {# 单词首字母大写 #}
    <p>{{ 'hello' | capitalize }}</p>
     
    {# 单词全小写 #}
    <p>{{ 'XML' | lower }}</p>
     
    {# 去除字符串前后的空白字符 #}
    <p>{{ '  hello  ' | trim }}</p>
     
    {# 字符串反转,返回"olleh" #}
    <p>{{ 'hello' | reverse }}</p>
     
    {# 格式化输出,返回"Number is 2" #}
    <p>{{ '%s is %d' | format("Number", 2) }}</p>
     
    {# 关闭HTML自动转义 #}
    <p>{{ '<em>name</em>' | safe }}</p>
     
    {% autoescape false %}
    {# HTML转义,即使autoescape关了也转义,可以缩写为e #}
    <p>{{ '<em>name</em>' | escape }}</p>
    {% endautoescape %}

    数值操作

    {# 四舍五入取整,返回13.0 #}
    <p>{{ 12.8888 | round }}</p>
     
    {# 向下截取到小数点后2位,返回12.88 #}
    <p>{{ 12.8888 | round(2, 'floor') }}</p>
     
    {# 绝对值,返回12 #}
    <p>{{ -12 | abs }}</p>

    列表操作

    {# 取第一个元素 #}
    <p>{{ [1,2,3,4,5] | first }}</p>
     
    {# 取最后一个元素 #}
    <p>{{ [1,2,3,4,5] | last }}</p>
     
    {# 返回列表长度,可以写为count #}
    <p>{{ [1,2,3,4,5] | length }}</p>
     
    {# 列表求和 #}
    <p>{{ [1,2,3,4,5] | sum }}</p>
     
    {# 列表排序,默认为升序 #}
    <p>{{ [3,2,1,5,4] | sort }}</p>
     
    {# 合并为字符串,返回"1 | 2 | 3 | 4 | 5" #}
    <p>{{ [1,2,3,4,5] | join(' | ') }}</p>
     
    {# 列表中所有元素都全大写。这里可以用upper,lower,但capitalize无效 #}
    <p>{{ ['tom','bob','ada'] | upper }}</p>

    字典列表操作

    {% set users=[{'name':'Tom','gender':'M','age':20},
                  {'name':'John','gender':'M','age':18},
                  {'name':'Mary','gender':'F','age':24},
                  {'name':'Bob','gender':'M','age':31},
                  {'name':'Lisa','gender':'F','age':19}]
    %}
     
     
    {# 按指定字段排序,这里设reverse为true使其按降序排 #}
    <ul>
    {% for user in users | sort(attribute='age', reverse=true) %}
         <li>{{ user.name }}, {{ user.age }}</li>
    {% endfor %}
    </ul>
     
    {# 列表分组,每组是一个子列表,组名就是分组项的值 #}
    <ul>
    {% for group in users|groupby('gender') %}
        <li>{{ group.grouper }}<ul>
        {% for user in group.list %}
            <li>{{ user.name }}</li>
        {% endfor %}</ul></li>
    {% endfor %}
    </ul>
     
    {# 取字典中的某一项组成列表,再将其连接起来 #}
    <p>{{ users | map(attribute='name') | join(', ') }}</p>

    Flask内置过滤器

    Flask提供了一个内置过滤器”tojson”,它的作用是将变量输出为JSON字符串。这个在配合Javascript使用时非常有用。我们延用上节字典列表操作中定义的”users”变量

  • 相关阅读:
    Socket接口(基于 Linux-2.4.0已更新)
    IP协议源码分析(基于linux-2.4.0已更新)
    udp_sendmsg源码完整分析(基于linux5.12.13版本内核)
    UDP详细理解(实现部分基于linux5.12.12版本内核)
    IP地址分配(静态分配+动态分配+零配置)
    计算机网络由哪些硬件设备组成?(基础收录)
    浅析C语言定义时赋值、定义后赋值、定义时不赋值
    《C指针全解》让你不再害怕指针
    makdown文字图片居中字体颜色表格列宽
    (C语言内存二十一)C语言变量的存储类别和生存期
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6991170.html
Copyright © 2011-2022 走看看