Flask框架學(xué)習(xí)筆記之模板操作實(shí)例詳解
本文實(shí)例講述了Flask框架學(xué)習(xí)筆記之模板操作。分享給大家供大家參考,具體如下:
flask的模板引擎是Jinja2。
引入模板的好處是增加程序的可讀性和易維護(hù)性,從而不用將一堆html代碼塞在視圖函數(shù)中。
還是以hello world為例。最基礎(chǔ)的調(diào)用模板修飾文本。
# 根網(wǎng)址
@app.route('/')
def index():
# return render_template("index.html")
# 可以給模板傳入文本content修飾
content = "Hello World!"
return render_template("index.html", content = content)
index模板,用{{}}表示變量。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<h1>Hello World!</h1>></!-->
<h1>{{ content }}</h1>
</body>
</html>

這里定義一個(gè)類以傳入變量。
class User(object):
def __init__(self, user_id, user_name):
self.user_id = user_id
self.user_name = user_name
傳參
# 通過(guò)調(diào)用類的實(shí)例方法給模板傳遞參數(shù)修飾
@app.route('/user')
def user_index():
user = User(520, "loli")# user_id, user_name
return render_template("user_index.html", user=user)
user_index模板,僅顯示user_name。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello {{ user.user_name }}</h1>
</body>
</html>

在模板中實(shí)現(xiàn)if語(yǔ)句
# 在模板中使用if語(yǔ)句
@app.route('/query_user/<user_id>')
def query_user(user_id):
user = None
# 如果傳入的id為520則調(diào)用實(shí)例
if int(user_id) == 520:
user = User(520, 'loli')
return render_template("user_id.html", user=user)
user_id模板,用{% %}包裹if語(yǔ)句,若user不為None(也就是傳入了name),則顯示if下語(yǔ)句,否則顯示else下語(yǔ)句。
最后一定要加上{% endif %}表示判斷語(yǔ)句結(jié)束。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if user %}
<h1>hello {{ user.user_name }}</h1>
{% else %}
<h1>no this user</h1>
{% endif %}
</body>
</html>


在模板中使用for循環(huán)語(yǔ)句
@app.route('/users')
def user_list():
users = []
for i in range(1, 11):
user = User(i, "loli" + str(i))
users.append(user)# 將user添加到users
return render_template("user_list.html", users = users)# 在模板中修飾users
user_list模板,同樣的,for循環(huán)語(yǔ)句也要用{% %}包裹起來(lái),需要用{% endfor %}表示for循環(huán)結(jié)束。這里傳入id和name兩個(gè)參數(shù)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for user in users %}
{{ user.user_id }} -- {{ user.user_name }}<br>
{% endfor %}
</body>
</html>

模板的繼承。模板繼承的初衷也是為了代碼更加簡(jiǎn)單,更易維護(hù),將相同部分的代碼提到一個(gè)基類模板,有點(diǎn)類似于類的繼承。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h1>I love you {{ user.user_name }}</h1>
</div>
{% block content %}
{% endblock %}
<div>
<h1>So much!</h1>
</div>
</body>
</html>
用<div>圈起來(lái)的是不可變的父模板,可改動(dòng)添加的部分在{% block content %}和{% endblock %}之間。
子模版,用{% extends "父模板" %} 表示從父模板繼承了代碼。在{% block content %}和{% endblock %}之間添加內(nèi)容。
{% extends "base.html" %}
{% block content %}
<h2>more than anyone</h2>
{% endblock %}
{% extends "base.html" %}
{% block content %}
<h2>more than anything</h2>
{% endblock %}
調(diào)用
# 模板繼承1
@app.route('/one')
def one_base():
user = User(520, 'loli')
return render_template("one_base.html", user=user)
# 模板繼承2
@app.route('/two')
def two_base():
user = User(520, 'loli')
return render_template("two_base.html", user=user)


可以看到子模版繼承了題頭和尾部,中間為子模版添加的內(nèi)容。
代碼
#-*- coding:utf-8 -*-
from flask import Flask, render_template# 導(dǎo)入render_template以使用模板
# 定義一個(gè)models導(dǎo)入一個(gè)有id和name的類
from models import User
app = Flask(__name__)
# 根網(wǎng)址
@app.route('/')
def index():
# return render_template("index.html")
# 可以給模板傳入文本content修飾
content = "Hello World!"
return render_template("index.html", content = content)
# 通過(guò)調(diào)用類的實(shí)例方法給模板傳遞參數(shù)修飾
@app.route('/user')
def user_index():
user = User(520, "loli")
return render_template("user_index.html", user=user)
# 在模板中使用if語(yǔ)句
@app.route('/query_user/<user_id>')
def query_user(user_id):
user = None
if int(user_id) == 520:
user = User(520, 'loli')
return render_template("user_id.html", user=user)
# 在模板中使用for循環(huán)語(yǔ)句
@app.route('/users')
def user_list():
users = []
for i in range(1, 11):
user = User(i, "loli" + str(i))
users.append(user)
return render_template("user_list.html", users = users)
# 模板繼承1
@app.route('/one')
def one_base():
user = User(520, 'loli')
return render_template("one_base.html", user=user)
# 模板繼承2
@app.route('/two')
def two_base():
user = User(520, 'loli')
return render_template("two_base.html", user=user)
if __name__ == '__main__':
app.run()
希望本文所述對(duì)大家基于flask框架的Python程序設(shè)計(jì)有所幫助。
- flask框架jinja2模板與模板繼承實(shí)例分析
- Flask模板引擎之Jinja2語(yǔ)法介紹
- Python的Flask框架標(biāo)配模板引擎Jinja2的使用教程
- Python的Flask框架中的Jinja2模板引擎學(xué)習(xí)教程
- Flask框架Jinjia模板常用語(yǔ)法總結(jié)
- 一個(gè)基于flask的web應(yīng)用誕生 使用模板引擎和表單插件(2)
- 詳解flask入門模板引擎
- Flask框架模板渲染操作簡(jiǎn)單示例
- Python Flask框架模板操作實(shí)例分析
- python Web flask 視圖內(nèi)容和模板實(shí)現(xiàn)代碼
- Flask框架模板繼承實(shí)現(xiàn)方法分析
- flask框架渲染Jinja模板與傳入模板變量操作詳解
相關(guān)文章
python 基于opencv 實(shí)現(xiàn)一個(gè)鼠標(biāo)繪圖小程序
這篇文章主要介紹了python 基于opencv 實(shí)現(xiàn)一個(gè)鼠標(biāo)繪圖小程序,幫助大家更好的理解和使用python的opencv庫(kù),感興趣的朋友可以了解下2020-12-12
python?Sweetviz探索性數(shù)據(jù)可視化分析庫(kù)使用特征詳解
這篇文章主要為大家介紹了python?Sweetviz探索性數(shù)據(jù)可視化分析庫(kù)特征使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python?VisPy庫(kù)高性能科學(xué)可視化圖形處理用法實(shí)例探究
VisPy是一個(gè)用于高性能科學(xué)可視化的Python庫(kù),它建立在現(xiàn)代圖形處理單元(GPU)上,旨在提供流暢、交互式的數(shù)據(jù)可視化體驗(yàn),本文將深入探討VisPy的基本概念、核心特性以及實(shí)際應(yīng)用場(chǎng)景,并通過(guò)豐富的示例代碼演示其強(qiáng)大的可視化能力2023-12-12
python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit)
這篇文章主要介紹了python3.8.3安裝教程及環(huán)境配置的詳細(xì)教程(64-bit),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
pandas讀取文件夾下所有excel文件的實(shí)現(xiàn)
最近需要做一個(gè)需求,要求匯總一個(gè)文件夾所有的excel文件,所以本文就來(lái)介紹一下pandas讀取文件夾下所有excel文件的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Python氣泡提示與標(biāo)簽的實(shí)現(xiàn)
這篇文章主要介紹了Python氣泡提示與標(biāo)簽的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

