字段對(duì)象 | 說(shuō)明 |
---|---|
字段對(duì)象 | 說(shuō)明 |
StringField | 文本字段 |
TextAreaField | 多行文本字段 |
PasswordField | 密碼文本字段 |
HiddenField | 隱藏文件字段 |
DateField | 文本字段,值為 datetime.date 文本格式 |
DateTimeField | 文本字段,值為 datetime.datetime 文本格式 |
IntegerField | 文本字段,值為整數(shù) |
DecimalField | 文本字段,值為decimal.Decimal |
FloatField | 文本字段,值為浮點(diǎn)數(shù) |
BooleanField | 復(fù)選框,值為 True 和 False |
RadioField | 一組復(fù)選框 |
SelectField | 下拉列表 |
SelectMutipleField | 下拉列表可選擇多個(gè)值 |
FileField | 文件上傳字段 |
SubmitField | 表單提交按鈕 |
FormField | 把表單作為字段嵌入另一個(gè)表單 |
FieldList | 一組指定類型的字段 |
WTForms常用驗(yàn)證函數(shù)
驗(yàn)證函數(shù) | 說(shuō)明 |
---|---|
DateRequired | 確保字段中有數(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)證輸入值不在可選列表中 |
CSRF_ENABLED是為了CSRF(跨站請(qǐng)求偽造)保護(hù)。 SECRET_KEY用來(lái)生成加密令牌,當(dāng)CSRF激活的時(shí)候,該設(shè)置會(huì)根據(jù)設(shè)置的密匙生成加密令牌。在HTML頁(yè)面中直接寫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,password return render_template('login.html',method=request.method)123456789
配置參數(shù)
app.config['SECRET_KEY'] = 'SECRET_KEY'1
模板頁(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>1234567891011121314 視圖函數(shù) #coding=utf-8 from flask import Flask,render_template,\ redirect,url_for,session,request,flash #導(dǎo)入wtf擴(kuò)展的表單類 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' #自定義表單類,文本字段、密碼字段、提交按鈕 class Login(FlaskForm): us = StringField(label=u'用戶名',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.data pswd = form.ps.data pswd2 = form.ps2.data print name,pswd,pswd2 return 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)
到此這篇關(guān)于Flask處理Web表單的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Flask處理Web表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:東莞 漢中 臨汾 廊坊 長(zhǎng)春 河池 德宏 重慶
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Flask處理Web表單的實(shí)現(xiàn)方法》,本文關(guān)鍵詞 Flask,處理,Web,表單,的,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。