最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flask處理Web表單的實(shí)現(xiàn)方法

 更新時(shí)間:2021年01月31日 15:20:20   作者:流芳  
這篇文章主要介紹了Flask處理Web表單的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

web表單是web應(yīng)用程序的基本功能。

它是HTML頁(yè)面中負(fù)責(zé)數(shù)據(jù)采集的部件。表單有三個(gè)部分組成:表單標(biāo)簽、表單域、表單按鈕。表單允許用戶輸入數(shù)據(jù),負(fù)責(zé)HTML頁(yè)面數(shù)據(jù)采集,通過(guò)表單將用戶輸入的數(shù)據(jù)提交給服務(wù)器。

在Flask中,為了處理web表單,我們一般使用Flask-WTF擴(kuò)展,它封裝了WTForms,并且它有驗(yàn)證表單數(shù)據(jù)的功能。

WTForms支持的HTML標(biāo)準(zhǔn)字段

字段對(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)證輸入值不在可選列表中

使用 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è)面中直接寫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

使用 Flask-WTF 實(shí)現(xiàn)表單

配置參數(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

西贡区| 乐东| 乌鲁木齐市| 宁武县| 昭觉县| 若尔盖县| 年辖:市辖区| 莱芜市| 庄浪县| 洞头县| 洞头县| 陕西省| 宁明县| 灌南县| 鲁甸县| 黄平县| 三原县| 元谋县| 新干县| 台北县| 南澳县| 浦江县| 金昌市| 盐城市| 黎川县| 翁源县| 临夏市| 滦南县| 木兰县| 朔州市| 太原市| 上杭县| 永康市| 太湖县| 临猗县| 东至县| 安国市| 琼海市| 宝清县| 北辰区| 贵德县|