溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

flask結(jié)合jinja2使用的方法是什么

發(fā)布時(shí)間:2023-03-14 11:34:38 來源:億速云 閱讀:125 作者:iii 欄目:開發(fā)技術(shù)

這篇“flask結(jié)合jinja2使用的方法是什么”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“flask結(jié)合jinja2使用的方法是什么”文章吧。

模板渲染

app.py

from flask import Flask,render_template,request
app = Flask(__name__)
 
@app.route('/')
def hello_world():
    return render_template('index.html')
 
@app.route('/blog/<int:blog_id>')
def blog(blog_id):
    page = request.args.get('page', default=1, type=int)
    return render_template('blog.html',id=blog_id,page=page)
 
if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>coleak's page</title>
</head>
<body>
<h2>START</h2>
<h3>coleak2</h3>
<h4>coleak3</h4>
<h5>coleak4</h5>
<h6>coleak5</h6>
<h2>END</h2>
</body>
</html>

blog.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>這里是第{{ id }}位博客主的第{{ page }}頁博客</h2>
</body>
</html>

效果測(cè)試 

http://10.133.5.113:8000
http://10.133.5.113:8000/blog/3
http://10.133.5.113:8000/blog/3?page=6

flask結(jié)合jinja2使用的方法是什么

flask結(jié)合jinja2使用的方法是什么

模板訪問變量屬性

app.py

from flask import Flask,render_template,request
app = Flask(__name__)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/blog/<int:blog_id>')
def blog(blog_id):
    page = request.args.get('page', default=1, type=int)
    return render_template('blog.html',id=blog_id,page=page)
 
if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>coleak's page</title>
</head>
<body>
<h2>START</h2>
<div><h2>welcome {{ user.username }}</h2></div>
<div><h2>你的別名是{{ person.username }},郵箱是{{ person["email"] }}</h2></div>
<h3>coleak2</h3>
<h4>coleak3</h4>
<h5>coleak4</h5>
<h6>coleak5</h6>
<h2>END</h2>
</body>
</html>

效果測(cè)試

flask結(jié)合jinja2使用的方法是什么

內(nèi)置過濾器的使用

可以將過濾器應(yīng)用于數(shù)據(jù)以對(duì)其進(jìn)行修改。 例如,sum 篩選器可以對(duì)數(shù)據(jù)求和,escape 篩選器對(duì)它們進(jìn)行轉(zhuǎn)義,sort 篩選器對(duì)它們進(jìn)行排序。

app.py

from flask import Flask,render_template,request
app = Flask(__name__)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/filter')
def filter():
    User1=user('coleak',-123.456)
    return render_template("filter.html",user=User1)
 
if __name__ == '__main__':
    app.run()

filter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>過濾器</title>
</head>
<body>
<div>{{ user.username }}長(zhǎng)度為{{ user.username|length }}</div>
<div>{{ user.email }}絕對(duì)值為{{ user.email|abs }}</div>
</body>
</html>

效果測(cè)試

flask結(jié)合jinja2使用的方法是什么

自定義過濾器

app.py

from flask import Flask,render_template,request
from datetime import datetime
app = Flask(__name__)
 
def my_filter(value,format="%Y年-%m月-%d日 %H時(shí):%M分"):
    return value.strftime(format)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
app.add_template_filter(my_filter,"time_filter")
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/filter')
def filter():
    mytime=datetime.now()
    User1=user('coleak',-123.456)
    return render_template("filter.html",user=User1,mytime=mytime)
 
if __name__ == '__main__':
    app.run()

filter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>過濾器</title>
</head>
<body>
<div>{{ mytime }}過濾后為{{ mytime|time_filter }}</div>
</body>
</html>

效果測(cè)試

flask結(jié)合jinja2使用的方法是什么

控制語句

app.py

from flask import Flask,render_template,request
from datetime import datetime
app = Flask(__name__)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/control')
def control():
    age=request.args.get('age')
    age=int (age)
    books=[{"name":"boo1",'price':12},{"name":"boo2",'price':18},{"name":"book3",'price':21}]
    return render_template('control.html',age=age,books=books)
 
if __name__ == '__main__':
    app.run()

control.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>控制語句</title>
</head>
<body>
<div>
    {% if age>18 %}
        <h3>可以進(jìn)入網(wǎng)吧</h3>
    {% elif age==18 %}
        <h3>家長(zhǎng)陪同下進(jìn)入網(wǎng)吧</h3>
    {% else %}
        <h3>不可以進(jìn)入網(wǎng)吧</h3>
    {% endif %}
</div>
<div>
    {% for book in books %}
        <p>名稱:{{ book.name }}</p>
        <p>價(jià)格:{{ book.price }}</p>
    {% endfor %}
</div>
</body>
</html>

 效果測(cè)試

flask結(jié)合jinja2使用的方法是什么

模板繼承

模板繼承是一項(xiàng)強(qiáng)大的功能,可減少代碼重復(fù)并改善代碼組織。 我們定義了一個(gè)基本模板,其他模板文件也從中繼承。 這些模板文件將覆蓋基本模板文件的特定塊。

app.py

from flask import Flask,render_template,request
from datetime import datetime
app = Flask(__name__)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/base')
def base():
    return render_template("base.html")
@app.route('/ch2')
def ch2():
    return render_template("ch2.html")
@app.route('/ch3')
def ch3():
    return render_template("ch3.html")
 
 
if __name__ == '__main__':
    app.run()

base.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>

ch2.html

{% extends "base.html" %}
{% block title %}
    ch2的標(biāo)題
{% endblock %}
{% block body %}
    <div>ch2的body</div>
{% endblock %}

ch2.html

{% extends "base.html" %}
{% block title %}
    ch3的標(biāo)題
{% endblock %}
{% block body %}
    <div>ch3的body</div>
{% endblock %}

加載靜態(tài)文件

結(jié)構(gòu)框架 

flask結(jié)合jinja2使用的方法是什么

add.py

from flask import Flask,render_template,request
from datetime import datetime
app = Flask(__name__)
 
class user:
    def __init__(self,username,email):
        self.username=username
        self.email=email
 
@app.route('/')
def hello_world():
    User=user('coleak','123@163.com')
    person={
        "username":"coleak",
        "email":"123@666.com"
    }
    return render_template('index.html',user=User,person=person)
 
@app.route('/static')
def static_use():
    return render_template("static.html")
 
if __name__ == '__main__':
    app.run()

static.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>static</title>
    <link rel="stylesheet" href="{{ url_for('static',filename=" rel="external nofollow" css/style.css") }}">
    <script src="{{ url_for('static',filename="js/myjs.js") }}"></script>
</head>
<body>
<img src="{{ url_for('static',filename="images/flask.jpg") }}"></img>
</body>
</html>

myjs.js

alert('coleak');

style.css

body{
    background-color: pink;
}

flask.jpg

flask結(jié)合jinja2使用的方法是什么

效果測(cè)試

flask結(jié)合jinja2使用的方法是什么

flask結(jié)合jinja2使用的方法是什么

以上就是關(guān)于“flask結(jié)合jinja2使用的方法是什么”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI