亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Jinja2過(guò)濾器的使用、控制語(yǔ)句示例詳解

 更新時(shí)間:2023年03月02日 16:02:27   作者:頭禿小程  
在Python中,如果需要對(duì)某個(gè)變量進(jìn)行處理,我們可以通過(guò)函數(shù)來(lái)實(shí)現(xiàn),這篇文章主要介紹了Jinja2過(guò)濾器的使用、控制語(yǔ)句,需要的朋友可以參考下

1.過(guò)濾器的使用

1.過(guò)濾器和測(cè)試器

在Python中,如果需要對(duì)某個(gè)變量進(jìn)行處理,我們可以通過(guò)函數(shù)來(lái)實(shí)現(xiàn)。在模板中,我們則是通過(guò)過(guò)濾器來(lái)實(shí)現(xiàn)的。過(guò)濾器本質(zhì)上也是函數(shù),但是在模板中使用的方式是通過(guò)管道符號(hào)(|)來(lái)調(diào)用的。例如有個(gè)字符串類型變量name,想要獲取他的長(zhǎng)度,則可以通過(guò) {name[length}}來(lái)獲取,Jinja2會(huì)把name當(dāng)做第一個(gè)參數(shù)傳給length過(guò)濾器底層對(duì)應(yīng)的函數(shù)。length是Jinja2內(nèi)置好的過(guò)濾器,Jinja2中內(nèi)置了許多好用過(guò)濾器,如果內(nèi)置過(guò)濾器不滿足需求,我們還可以自定義過(guò)濾器。我們先來(lái)學(xué)習(xí)下如何自定義過(guò)濾器,讀者明白了過(guò)濾器的原理后,再去學(xué)習(xí)Jinja2內(nèi)置過(guò)濾器就更能得心應(yīng)手了。

2.過(guò)濾器

templates/filter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>過(guò)濾器使用demo</title>
</head>
<body>
{{ user.username}}-{{ user.username|length }}
</body>
</html>

app.py

# render_template 渲染模板
from flask import Flask, render_template
 
app = Flask(__name__)
 
class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email
 
@app.route("/filter")
def filter_demo():
    user = User(username="小程xxx", email="xxx@qq.com")
    return render_template("filter.html",user=user)
 
if __name__ == '__main__':
    app.run()

效果

3.自定義過(guò)濾器

       過(guò)濾器本質(zhì)上是 Python的函數(shù),他會(huì)把被過(guò)濾的值當(dāng)做第一個(gè)參數(shù)傳給這個(gè)函數(shù),函數(shù)經(jīng)過(guò)一些邏輯處理后,再返回新的值。在過(guò)濾器函數(shù)寫好后,可以通過(guò)@app.template_ filter裝飾器或者是app.add_template_filter函數(shù)來(lái)把函數(shù)注冊(cè)成Jinja2能用的過(guò)濾器。這里我們以注冊(cè)一個(gè)時(shí)間格式化的過(guò)濾器為例,來(lái)說(shuō)明下自定義過(guò)濾器的方法。

app.py

# render_template 渲染模板
from flask import Flask, render_template
from datetime import datetime
 
app = Flask(__name__)
 
# strftime:根據(jù)區(qū)域設(shè)置格式化本地時(shí)間
# format:格式化
def datetime_format(value,format="%Y年%m月%d日 %H:%m"):
    return value.strftime(format)
 
app.add_template_filter(datetime_format,"dformat")
 
class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email
 
 
# filter:過(guò)濾器
@app.route("/filter")
def filter_demo():
    user = User(username="小程xxx", email="xxx@qq.com")
    mytime=datetime.now()
    return render_template("filter.html",user=user,mytime=mytime)
 
if __name__ == '__main__':
    app.run()

       上面我們定義了一個(gè)datetime_formt的函數(shù),第一個(gè)參數(shù)是需要被處理的值,第二個(gè)參數(shù)是時(shí)間的格式,并且指定了一個(gè)默認(rèn)值。然后下面通過(guò)app.add_template_filter,將datetime_format函數(shù)注冊(cè)成了過(guò)濾器,并且這個(gè)過(guò)濾器的名字,叫做dformat。那么以后在模板文件中,就可以這樣類似這樣使用了:

templates/filter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>過(guò)濾器使用demo</title>
</head>
<body>
<div>{{ user.username}}-{{ user.username|length }}</div>
<div>{{ mytime|dformat }}</div>
</body>
</html>

效果 

 2.控制語(yǔ)句

1.if

app.py 

# render_template 渲染模板
from flask import Flask, render_template
 
app = Flask(__name__)
 
@app.route("/control")
def control_statement():
    age=17
    return render_template("control.html",age=age)
 
if __name__ == '__main__':
    app.run()

templates/control.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% if age>18 %}
    <div>您已經(jīng)滿118歲,可以進(jìn)入網(wǎng)吧!</div>
{% elif age==18 %}
    <div>您剛滿18歲,需要父母陪同才能進(jìn)入!</div>
{% else %}
    <div>您未滿18歲,不能進(jìn)入網(wǎng)吧!</div>
{% endif %}
</body>
</html>

注:

  • 可以注意到if語(yǔ)句結(jié)束后,需要加一個(gè)endif來(lái)關(guān)閉if代碼塊。這個(gè)跟python是有點(diǎn)不同的。
  • Jinja2中的代碼縮進(jìn)只是為了更加方便閱讀。任何縮進(jìn)都不是必須的。

2.for 

app.py

# render_template 渲染模板
from flask import Flask, render_template
 
app = Flask(__name__)
 
@app.route("/control")
def control_statement():
    age = 17
    books = [{
        "name": "三國(guó)演義",
        "author": "羅貫中"
    },{
        "name": "水滸傳",
        "author": "施耐庵"
    }
    ]
    return render_template("control.html", age=age,books=books)
if __name__ == '__main__':
    app.run()

templates/control.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 
{% for book in books %}
    <div>圖書名稱:{{ book.name }},圖書作者:{{ book.author }}</div>
{% endfor %}
 
</body>
</html>

相關(guān)文章

最新評(píng)論