Flask處理Web表單的實現(xiàn)方法
web表單是web應(yīng)用程序的基本功能。
它是HTML頁面中負責數(shù)據(jù)采集的部件。表單有三個部分組成:表單標簽、表單域、表單按鈕。表單允許用戶輸入數(shù)據(jù),負責HTML頁面數(shù)據(jù)采集,通過表單將用戶輸入的數(shù)據(jù)提交給服務(wù)器。
在Flask中,為了處理web表單,我們一般使用Flask-WTF擴展,它封裝了WTForms,并且它有驗證表單數(shù)據(jù)的功能。
WTForms支持的HTML標準字段
字段對象 | 說明 |
---|---|
字段對象 | 說明 |
StringField | 文本字段 |
TextAreaField | 多行文本字段 |
PasswordField | 密碼文本字段 |
HiddenField | 隱藏文件字段 |
DateField | 文本字段,值為 datetime.date 文本格式 |
DateTimeField | 文本字段,值為 datetime.datetime 文本格式 |
IntegerField | 文本字段,值為整數(shù) |
DecimalField | 文本字段,值為decimal.Decimal |
FloatField | 文本字段,值為浮點數(shù) |
BooleanField | 復(fù)選框,值為 True 和 False |
RadioField | 一組復(fù)選框 |
SelectField | 下拉列表 |
SelectMutipleField | 下拉列表可選擇多個值 |
FileField | 文件上傳字段 |
SubmitField | 表單提交按鈕 |
FormField | 把表單作為字段嵌入另一個表單 |
FieldList | 一組指定類型的字段 |
WTForms常用驗證函數(shù)
驗證函數(shù) | 說明 |
---|---|
DateRequired | 確保字段中有數(shù)據(jù) |
EqualTo | 比較兩個字段的值,常用于比較兩次密碼的輸入 |
Length | 驗證輸入的字符串長度 |
NumberRange | 驗證輸入的值在數(shù)字范圍內(nèi) |
URL | 驗證URL |
AnyOf | 驗證輸入值在可選列表中 |
NoneOf | 驗證輸入值不在可選列表中 |
使用 Flask-WTF 需要配置參數(shù) SECRET_KEY
CSRF_ENABLED是為了CSRF(跨站請求偽造)保護。 SECRET_KEY用來生成加密令牌,當CSRF激活的時候,該設(shè)置會根據(jù)設(shè)置的密匙生成加密令牌。在HTML頁面中直接寫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 實現(xiàn)表單
配置參數(shù)
app.config['SECRET_KEY'] = 'SECRET_KEY'1
模板頁面
<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擴展的表單類 from flask_wtf import FlaskForm #導(dǎo)入自定義表單需要的字段 from wtforms import SubmitField,StringField,PasswordField #導(dǎo)入wtf擴展提供的表單驗證器 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'確認密碼',validators=[DataRequired()]) submit = SubmitField(u'提交') @app.route('/login') def login(): return render_template('login.html') #定義根路由視圖函數(shù),生成表單對象,獲取表單數(shù)據(jù),進行表單數(shù)據(jù)驗證 @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'信息有誤,請重新輸入!') print form.validate_on_submit() return render_template('index.html',form=form) if __name__ == '__main__': app.run(debug=True)
到此這篇關(guān)于Flask處理Web表單的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)Flask處理Web表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用python基于appium模塊開發(fā)一個自動收取能量的小助手
大家都有了解過螞蟻森林吧,本篇文章帶給你自動收取螞蟻森林能量的思路與方法,基于appium模塊開發(fā)一個自動收取能量的小助手,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的價值2021-09-09Python使用MySQL8.2讀寫分離實現(xiàn)示例詳解
在這篇文章中,我們將了解如何將?MySQL?8.2?的讀寫分離功能與?MySQL-Connector/Python?一起使用的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11使用Python創(chuàng)建一個視頻管理器并實現(xiàn)視頻截圖功能
在這篇博客中,我將向大家展示如何使用 wxPython 創(chuàng)建一個簡單的圖形用戶界面 (GUI) 應(yīng)用程序,該應(yīng)用程序可以管理視頻文件列表、播放視頻,并生成視頻截圖,我們將逐步實現(xiàn)這些功能,并確保代碼易于理解和擴展,感興趣的小伙伴跟著小編一起來看看吧2024-08-08解決python3.x安裝numpy成功但import出錯的問題
這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11