flask 過濾器與管道命令,url_for,消息閃現flash,變量作用域

jinja2模板與python的語句不同
  1. {{ data | length() }} {# 得到data的長度 #}
  2. {{ data.name | default(‘wangyudong’) }}
    {{ data.name == None | default(‘wangyudong’) }} {# True #}
  3. default 判斷屬性是否存在的,而不判斷空值,不存在則把default的值賦給模板
  4. 符號 | 類似Linux的管道命令,前面值向後的傳遞
{% extends 'layout.html' %}
{% block content %}
    {{ super() }}  {# 繼承,保留原來的content內容 #}
    {{ data.age }}
    {{ data['age'] }}
    {# if 條件判斷 #}
    {% if data.age < 20 %}
        {{ data.name }}
    {% elif data.age == 20 %}
        do something
        {{ data | length() }}  {# 得到data的長度 #}
        {{ data.name | default('wangyudong') }}
        {{ data.name == None | default('wangyudong') }} {# True #}
        {# default 判斷屬性是否存在的,而不判斷空值 #}
{#        | 類似Linux的管道命令,前面值向後的傳遞 #}
    {% else %}
        {{ data.age }}
        <ul></ul>
    {% endif %}
    {# for in 循環 #}
    {% for foo in [1, 2, 3] %}
        {{ foo }}
        <div>111</div>
    {% endfor %}
{% endblock %}

url_for

反向生成url需要用到endpoint端點

在這裏插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {# 使用endpoint:static生成url #}
    <link rel="stylesheet" href="{{ url_for('static', filename='test.css') }}">
</head>
<body>
    {# 定義三個區域 #}
    {% block head %}
        <div>this is head</div>
    {% endblock %}
    {% block content %}
        this is content
        {# 待替換的部分 #}
    {% endblock %}
    {% block foot %}
        <div>this is foot</div>
    {% endblock %}
</body>
</html>

url_for 和 render_template區別:
url_for : 是通過文件的路由尋找static文件
render_template() : 是通過傳入文件找template文件

flash消息閃現(需要配置secret_key密鑰)
加密 session 需要設置 secret_key 唯一併保密(獨一無二的)

flash()方法接收數據

# 設置flash

from flask import flash, render_template

@web.route('/test')
def test():
    r = {
        'name': '',
        'age': 20
    }
    flash('hello,wang')
    return render_template('test.html', data=r)

得到flash數據並顯示
{% set messages = get_flashed_messages() %}
{{ messages }}

# 設置一個變量接收flash數據,然後顯示flash的數據

{% extends 'layout.html' %}
{% block content %}
    {{ super() }}  {# 繼承,保留原來的content內容 #}
    {% if data.age < 20 %}
        {{ data.name }}
    {% elif data.age == 20 %}
        {{ data.name == None | default('wangyudong') }} {# True #}
        {# 設置變量用set  和 {% %} 執行語句符號 #}
        {# 顯示變量用 {{ }}  符號 #} 
        {% set messages = get_flashed_messages() %}
        {{ messages }}
    {% else %}
        {{ data.age }}
        <ul></ul>
    {% endif %}
{% endblock %}

返回值是一個 list 列表
this is head
this is content False [‘hello,wang’]
this is foot

所以flask可以多次調用

@web.route('/test')
def test():
    r = {
        'name': '',
        'age': 20
    }
    flash('hello,wang')
    flash('你好,九月')
    return render_template('test.html', data=r)

this is head
this is content False [‘hello,wang’, ‘你好,九月’]
this is foot

可以設置category來設置flash消息的優先級

@web.route('/test')
def test():
    r = {
        'name': '',
        'age': 20
    }
    flash('hello,wang', category='error')
    flash('你好,九月', category='warning')
    return render_template('test.html', data=r)

過濾category:
在調用 get_flashed_messages() 方法的時候需要傳入一個 category_filter = [‘error’] 參數,category_filter 數組內部設置想要獲取的flash語句的級別

{% extends 'layout.html' %}
{% block content %}
    {{ super() }}  {# 繼承,保留原來的content內容 #}
    {% if data.age < 20 %}
        {{ data.name }}
    {% elif data.age == 20 %}
        {{ data.name == None | default('wangyudong') }} {# True #}
        {# 只獲取error級別的flash #}
        {% with messages = get_flashed_messages(category_filter=['error']) %}
        {{ messages }}
        {% endwith %}
    {% else %}
        {{ data.age }}
        <ul></ul>
    {% endif %}
{% endblock %}

輸出
this is head
this is content False [‘hello,wang’]
this is foot

變量作用域

set 和 with 的作用域不同:

set 定義的massages變量在這個block區域都是有效的可使用的
with 定義的messages變量只在with和endwith之間可訪問

set:不用閉合(不用end),在整個block都可以使用

# 在content這個block區域可以使用messages變量
{% extends 'layout.html' %}
{% block content %}
    {{ super() }}  {# 繼承,保留原來的content內容 #}
    {% if data.age < 20 %}
        {{ data.name }}
    {% elif data.age == 20 %}
        {{ data.name == None | default('wangyudong') }} {# True #}
        {# set 定義的massages變量在這個block區域都是有效的可使用的 #}
        {% set messages = get_flashed_messages() %}
        {{ messages }}
    {% else %}
        {{ data.age }}
        <ul></ul>
    {% endif %}
{% endblock %}

# 在foot這個block區域不能使用messages變量
{% block foot %}
    {{ messages }}
{% endblock %}

with:需要閉合(endwith)只能在with和endwith之間使用

{% extends 'layout.html' %}
{% block content %}
    {{ super() }}  {# 繼承,保留原來的content內容 #}
    {% if data.age < 20 %}
        {{ data.name }}
    {% elif data.age == 20 %}
        {{ data.name == None | default('wangyudong') }} {# True #}
        {# set 定義的massages變量在這個block區域都是有效的可使用的 #}
        {# with 定義的messages變量只在with和endwith之間可訪問 #}
        {% with messages = get_flashed_messages() %}
        {{ messages }}
        {% endwith %}
    {% else %}
        {{ data.age }}
        <ul></ul>
    {% endif %}
{% endblock %}

得到的messages可以使用 for in 來遍歷得到所有數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章