Flask模板01
模板
在前面的示例中,視圖函數(shù)的主要作用是生成請(qǐng)求的響應(yīng),這是最簡(jiǎn)單的請(qǐng)求。實(shí)際上,視圖函數(shù)有兩個(gè)作用:處理業(yè)務(wù)邏輯和返回響應(yīng)內(nèi)容。在大型應(yīng)用中,把業(yè)務(wù)邏輯和表現(xiàn)內(nèi)容放在一起,會(huì)增加代碼的復(fù)雜度和維護(hù)成本。本節(jié)學(xué)到的模板,它的作用即是承擔(dān)視圖函數(shù)的另一個(gè)作用,即返回響應(yīng)內(nèi)容。 模板其實(shí)是一個(gè)包含響應(yīng)文本的文件,其中用占位符(變量)表示動(dòng)態(tài)部分,告訴模板引擎其具體值需要從使用的數(shù)據(jù)中獲取。使用真實(shí)值替換變量,再返回最終得到的字符串,這個(gè)過(guò)程稱(chēng)為“渲染”。Flask使用Jinja2這個(gè)模板引擎來(lái)渲染模板。Jinja2能識(shí)別所有類(lèi)型的變量,包括{}。 Jinja2模板引擎,Flask提供的render_template函數(shù)封裝了該模板引擎,render_template函數(shù)的第一個(gè)參數(shù)是模板的文件名,后面的參數(shù)都是鍵值對(duì),表示模板中變量對(duì)應(yīng)的真實(shí)值。
Jinja2官方文檔(http://docs.jinkan.org/docs/jinja2/)
我們先來(lái)認(rèn)識(shí)下模板的基本語(yǔ)法:
{% if user %}{{ user }} {% else %}hello! <ul>{% for index in indexs %}<li> {{ index }} </li>{% endfor %} </ul>通過(guò)修改一下前面的示例,來(lái)學(xué)習(xí)下模板的簡(jiǎn)單使用:
@app.route('/') def hello_itcast():return render_template('index.html')@app.route('/user/<name>') def hello_user(name):return render_template('index.html',name=name)變量
在模板中{{ variable }}結(jié)構(gòu)表示變量,是一種特殊的占位符,告訴模板引擎這個(gè)位置的值,從渲染模板時(shí)使用的數(shù)據(jù)中獲取;Jinja2除了能識(shí)別基本類(lèi)型的變量,還能識(shí)別{};
<p>{{mydict['key']}}</p><p>{{mylist[1]}}</p><p>{{mylist[myvariable]}}</p> from flask import Flask,render_template app = Flask(__name__)@app.route('/') def index():mydict = {'key':'silence is gold'}mylist = ['Speech', 'is','silver']myintvar = 0return render_template('vars.html',mydict=mydict,mylist=mylist,myintvar=myintvar) if __name__ == '__main__':app.run(debug=True)反向路由:
Flask提供了url_for()輔助函數(shù),可以使用程序URL映射中保存的信息生成URL;url_for()接收視圖函數(shù)名作為參數(shù),返回對(duì)應(yīng)的URL;
如調(diào)用url_for('index',_external=True)返回的是絕對(duì)地址,在下面這個(gè)示例中是http://127.0.0.1:5000/index。
@app.route('/index') def index():return render_template('index.html')@app.route('/user/') def redirect():return url_for('index',_external=True)自定義錯(cuò)誤頁(yè)面:
from flask import Flask,render_template@app.errorhandler(404) def page_not_found(e):return render_template('404.html'), 4043.2 過(guò)濾器:
過(guò)濾器的本質(zhì)就是函數(shù)。有時(shí)候我們不僅僅只是需要輸出變量的值,我們還需要修改變量的顯示,甚至格式化、運(yùn)算等等,這就用到了過(guò)濾器。 過(guò)濾器的使用方式為:變量名 | 過(guò)濾器。 過(guò)濾器名寫(xiě)在變量名后面,中間用 | 分隔。如:{{variable | capitalize}},這個(gè)過(guò)濾器的作用:把變量variable的值的首字母轉(zhuǎn)換為大寫(xiě),其他字母轉(zhuǎn)換為小寫(xiě)。 其他常用過(guò)濾器如下:
字符串操作:
safe:禁用轉(zhuǎn)義;
<p>{{ '<em>hello</em>' | safe }}</p>capitalize:把變量值的首字母轉(zhuǎn)成大寫(xiě),其余字母轉(zhuǎn)小寫(xiě);
<p>{{ 'hello' | capitalize }}</p>lower:把值轉(zhuǎn)成小寫(xiě);
<p>{{ 'HELLO' | lower }}</p>upper:把值轉(zhuǎn)成大寫(xiě);
<p>{{ 'hello' | upper }}</p>title:把值中的每個(gè)單詞的首字母都轉(zhuǎn)成大寫(xiě);
<p>{{ 'hello' | title }}</p>trim:把值的首尾空格去掉;
<p>{{ ' hello world ' | trim }}</p>reverse:字符串反轉(zhuǎn);
<p>{{ 'olleh' | reverse }}</p>format:格式化輸出;
<p>{{ '%s is %d' | format('name',17) }}</p>striptags:渲染之前把值中所有的HTML標(biāo)簽都刪掉;
<p>{{ '<em>hello</em>' | striptags }}</p>列表操作
first:取第一個(gè)元素
<p>{{ [1,2,3,4,5,6] | first }}</p>last:取最后一個(gè)元素
<p>{{ [1,2,3,4,5,6] | last }}</p>length:獲取列表長(zhǎng)度
<p>{{ [1,2,3,4,5,6] | length }}</p>sum:列表求和
<p>{{ [1,2,3,4,5,6] | sum }}</p>sort:列表排序
<p>{{ [6,2,3,1,5,4] | sort }}</p>語(yǔ)句塊過(guò)濾(不常用):
{% filter upper %}this is a Flask Jinja2 introduction{% endfilter %}自定義過(guò)濾器:
過(guò)濾器的本質(zhì)是函數(shù)。當(dāng)模板內(nèi)置的過(guò)濾器不能滿(mǎn)足需求,可以自定義過(guò)濾器。自定義過(guò)濾器有兩種實(shí)現(xiàn)方式:一種是通過(guò)Flask應(yīng)用對(duì)象的add_template_filter方法。還可以通過(guò)裝飾器來(lái)實(shí)現(xiàn)自定義過(guò)濾器。
自定義的過(guò)濾器名稱(chēng)如果和內(nèi)置的過(guò)濾器重名,會(huì)覆蓋內(nèi)置的過(guò)濾器。
實(shí)現(xiàn)方式一:通過(guò)調(diào)用應(yīng)用程序?qū)嵗腶dd_template_filter方法實(shí)現(xiàn)自定義過(guò)濾器。該方法第一個(gè)參數(shù)是函數(shù)名,第二個(gè)參數(shù)是自定義的過(guò)濾器名稱(chēng)。
def filter_double_sort(ls):return ls[::2] app.add_template_filter(filter_double_sort,'double_2')實(shí)現(xiàn)方式二:用裝飾器來(lái)實(shí)現(xiàn)自定義過(guò)濾器。裝飾器傳入的參數(shù)是自定義的過(guò)濾器名稱(chēng)。
@app.template_filter('db3') def filter_double_sort(ls):return ls[::-3]3.3 Web表單:
web表單是web應(yīng)用程序的基本功能。
它是HTML頁(yè)面中負(fù)責(zé)數(shù)據(jù)采集的部件。表單有三個(gè)部分組成:表單標(biāo)簽、表單域、表單按鈕。表單允許用戶(hù)輸入數(shù)據(jù),負(fù)責(zé)HTML頁(yè)面數(shù)據(jù)采集,通過(guò)表單將用戶(hù)輸入的數(shù)據(jù)提交給服務(wù)器。
在Flask中,為了處理web表單,我們一般使用Flask-WTF擴(kuò)展,它封裝了WTForms,并且它有驗(yàn)證表單數(shù)據(jù)的功能。
WTForms支持的HTML標(biāo)準(zhǔn)字段
| StringField | 文本字段 |
| TextAreaField | 多行文本字段 |
| PasswordField | 密碼文本字段 |
| HiddenField | 隱藏文本字段 |
| DateField | 文本字段,值為datetime.date格式 |
| DateTimeField | 文本字段,值為datetime.datetime格式 |
| IntegerField | 文本字段,值為整數(shù) |
| DecimalField | 文本字段,值為decimal.Decimal |
| FloatField | 文本字段,值為浮點(diǎn)數(shù) |
| BooleanField | 復(fù)選框,值為T(mén)rue和False |
| RadioField | 一組單選框 |
| SelectField | 下拉列表 |
| SelectMultipleField | 下拉列表,可選擇多個(gè)值 |
| FileField | 文本上傳字段 |
| SubmitField | 表單提交按鈕 |
| FormField | 把表單作為字段嵌入另一個(gè)表單 |
| FieldList | 一組指定類(lèi)型的字段 |
WTForms常用驗(yàn)證函數(shù)
| DataRequired | 確保字段中有數(shù)據(jù) |
| EqualTo | 比較兩個(gè)字段的值,常用于比較兩次密碼輸入 |
| Length | 驗(yàn)證輸入的字符串長(zhǎng)度 |
| NumberRange | 驗(yàn)證輸入的值在數(shù)字范圍內(nèi) |
| URL | 驗(yàn)證URL |
| AnyOf | 驗(yàn)證輸入值在可選列表中 |
| NoneOf | 驗(yàn)證輸入值不在可選列表中 |
使用Flask-WTF需要配置參數(shù)SECRET_KEY。
CSRF_ENABLED是為了CSRF(跨站請(qǐng)求偽造)保護(hù)。 SECRET_KEY用來(lái)生成加密令牌,當(dāng)CSRF激活的時(shí)候,該設(shè)置會(huì)根據(jù)設(shè)置的密匙生成加密令牌。
在HTML頁(yè)面中直接寫(xiě)form表單:
#模板文件 <form method='post'><input type="text" name="username" placeholder='Username'><input type="password" name="password" placeholder='password'><input type="submit"> </form>視圖函數(shù)中獲取表單數(shù)據(jù):
from flask import Flask,render_template,request@app.route('/login',methods=['GET','POST']) def login():if request.method == 'POST':username = request.form['username']password = request.form['password']print username,passwordreturn render_template('login.html',method=request.method)使用Flask-WTF實(shí)現(xiàn)表單。
配置參數(shù):
app.config['SECRET_KEY'] = 'silents is gold'模板頁(yè)面:
<form method="post">#設(shè)置csrf_token{{ form.csrf_token() }}{{ form.us.label }}<p>{{ form.us }}</p>{{ form.ps.label }}<p>{{ form.ps }}</p>{{ form.ps2.label }}<p>{{ form.ps2 }}</p><p>{{ form.submit() }}</p>{% for x in get_flashed_messages() %}{{ x }}{% endfor %}</form>視圖函數(shù):
#coding=utf-8 from flask import Flask,render_template,\redirect,url_for,session,request,flash#導(dǎo)入wtf擴(kuò)展的表單類(lèi) from flask_wtf import FlaskForm #導(dǎo)入自定義表單需要的字段 from wtforms import SubmitField,StringField,PasswordField #導(dǎo)入wtf擴(kuò)展提供的表單驗(yàn)證器 from wtforms.validators import DataRequired,EqualTo app = Flask(__name__) app.config['SECRET_KEY']='1'#自定義表單類(lèi),文本字段、密碼字段、提交按鈕 class Login(Form):us = StringField(label=u'用戶(hù):',validators=[DataRequired()])ps = PasswordField(label=u'密碼',validators=[DataRequired(),EqualTo('ps2','err')])ps2 = PasswordField(label=u'確認(rèn)密碼',validators=[DataRequired()])submit = SubmitField(u'提交')@app.route('/login') def login():return render_template('login.html')#定義根路由視圖函數(shù),生成表單對(duì)象,獲取表單數(shù)據(jù),進(jìn)行表單數(shù)據(jù)驗(yàn)證 @app.route('/',methods=['GET','POST']) def index():form = Login()if form.validate_on_submit():name = form.us.datapswd = form.ps.datapswd2 = form.ps2.dataprint name,pswd,pswd2return redirect(url_for('login'))else:if request.method=='POST':flash(u'信息有誤,請(qǐng)重新輸入!')print form.validate_on_submit()return render_template('index.html',form=form) if __name__ == '__main__':app.run(debug=True)總結(jié)
- 上一篇: 项目立项和支付要求
- 下一篇: Day08-函数(3)