flask結(jié)合jinja2使用詳解
jinja2簡介
特征
- 沙箱中執(zhí)行
- 強(qiáng)大的 HTML 自動轉(zhuǎn)義系統(tǒng)保護(hù)系統(tǒng)免受 XSS
- 模板繼承
- 及時編譯最優(yōu)的 python 代碼
- 可選提前編譯模板的時間
- 易于調(diào)試。異常的行數(shù)直接指向模板中的對應(yīng)行。
- 可配置的語法
Jinja 分隔符
Jinja 在模板字符串中使用各種分隔符。
{% %}
-陳述{{ }}
-要打印到模板輸出的表達(dá)式{# #}
-模板輸出中未包含的注釋# ##
-行語句
模板渲染
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> <h1>START</h1> <h2>coleak2</h2> <h3>coleak3</h3> <h4>coleak4</h4> <h5>coleak5</h5> <h1>END</h1> </body> </html>
blog.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>這里是第{{ id }}位博客主的第{{ page }}頁博客</h1> </body> </html>
效果測試
http://10.133.5.113:8000
http://10.133.5.113:8000/blog/3
http://10.133.5.113:8000/blog/3?page=6
模板訪問變量屬性
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> <h1>START</h1> <div><h1>welcome {{ user.username }}</h1></div> <div><h1>你的別名是{{ person.username }},郵箱是{{ person["email"] }}</h1></div> <h2>coleak2</h2> <h3>coleak3</h3> <h4>coleak4</h4> <h5>coleak5</h5> <h1>END</h1> </body> </html>
效果測試
內(nèi)置過濾器的使用
可以將過濾器應(yīng)用于數(shù)據(jù)以對其進(jìn)行修改。 例如,sum 篩選器可以對數(shù)據(jù)求和,escape 篩選器對它們進(jìn)行轉(zhuǎn)義,sort 篩選器對它們進(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 }}長度為{{ user.username|length }}</div> <div>{{ user.email }}絕對值為{{ user.email|abs }}</div> </body> </html>
效果測試
自定義過濾器
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時:%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>
效果測試
控制語句
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 %} <h2>可以進(jìn)入網(wǎng)吧</h2> {% elif age==18 %} <h2>家長陪同下進(jìn)入網(wǎng)吧</h2> {% else %} <h2>不可以進(jìn)入網(wǎng)吧</h2> {% endif %} </div> <div> {% for book in books %} <p>名稱:{{ book.name }}</p> <p>價格:{{ book.price }}</p> {% endfor %} </div> </body> </html>
效果測試
模板繼承
模板繼承是一項強(qiáng)大的功能,可減少代碼重復(fù)并改善代碼組織。 我們定義了一個基本模板,其他模板文件也從中繼承。 這些模板文件將覆蓋基本模板文件的特定塊。
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('/ch1') def ch1(): return render_template("ch1.html") @app.route('/ch2') def ch2(): return render_template("ch2.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>
ch1.html
{% extends "base.html" %} {% block title %} ch1的標(biāo)題 {% endblock %} {% block body %} <div>ch1的body</div> {% endblock %}
ch1.html
{% extends "base.html" %} {% block title %} ch2的標(biāo)題 {% endblock %} {% block body %} <div>ch2的body</div> {% endblock %}
加載靜態(tài)文件
結(jié)構(gòu)框架
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
效果測試
到此這篇關(guān)于flask結(jié)合jinja2使用詳解的文章就介紹到這了,更多相關(guān)flask jinja2使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用20行代碼實現(xiàn)微信聊天機(jī)器人
這篇文章主要介紹了Python使用20行代碼實現(xiàn)微信聊天機(jī)器人,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Numpy數(shù)組的轉(zhuǎn)置和軸交換的實現(xiàn)
本文主要介紹了Numpy數(shù)組的轉(zhuǎn)置和軸交換的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03在Django中動態(tài)地過濾查詢集的實現(xiàn)
本文主要介紹了Django中動態(tài)地過濾查詢集的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Python大數(shù)據(jù)之使用lxml庫解析html網(wǎng)頁文件示例
這篇文章主要介紹了Python大數(shù)據(jù)之使用lxml庫解析html網(wǎng)頁文件,結(jié)合實例形式分析了Python大數(shù)據(jù)操作中使用lxml庫解析html網(wǎng)頁具體步驟及相關(guān)注意事項,需要的朋友可以參考下2019-11-11Django權(quán)限系統(tǒng)auth模塊用法解讀
這篇文章主要介紹了Django權(quán)限系統(tǒng)auth模塊用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05