Tyrion是一個基於Python實現的支持多個WEB框架的Form表單驗證組件,其完美的支持Tornado、Django、Flask、Bottle Web框架。Tyrion主要有兩大重要動能:html
對於表單驗證,告別書寫重複的正則表達式對用戶提交的數據進行驗證的工做,今後解放雙手,跟着我左手右手一個慢動做...前端
對於生成HTML標籤,不在人工書寫html標籤,讓Tyrion幫你自動建立...python
對於保留上次提交內容,因爲默認表單提交後頁面刷新,原來輸入的內容會清空,Tyrion能夠保留上次提交內容。git
github:https://github.com/WuPeiqi/Tyriongithub
一、下載安裝web
pip install PyTyrion
github: https://github.com/WuPeiqi/Tyrion正則表達式
二、配置WEB框架種類數據庫
因爲Tyrion同時支持Tornado、Django、Flask、Bottle多個WEB框架,全部在使用前須要進行指定。 django
import Tyrion Tyrion.setup('tornado') # setup的參數有:tornado(默認)、django、bottle、flask
三、建立Form類flask
Form類用於提供驗證規則、插件屬性、錯誤信息等
from Tyrion.Forms import Form from Tyrion.Fields import StringField from Tyrion.Fields import EmailField class LoginForm(Form): username = StringField(error={'required': '用戶名不能爲空'}) password = StringField(error={'required': '密碼不能爲空'}) email = EmailField(error={'required': '郵箱不能爲空', 'invalid': '郵箱格式錯誤'})
四、驗證用戶請求
前端HTML代碼:
<form method="POST" action="/login.html"> <div> <input type="text" name="username"> </div> <div> <input type="text" name="password"> </div> <div> <input type="text" name="email"> </div> <input type="submit" value="提交"> </form>
用戶提交數據時,在後臺書寫以下代碼便可實現用戶請求數據驗證(Tornado示例):
class LoginHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): self.render('login.html') def post(self, *args, **kwargs): form = LoginForm(self) ###### 檢查用戶輸入是否合法 ###### if form.is_valid(): ###### 若是不合法,則輸出錯誤信息 ###### print(form.error_dict) else: ###### 若是合法,則輸出用戶輸入的內容 ###### print(form.value_dict) self.render('login.html')
示例01:源碼下載(含Tornado、Django、Flask、Bottle)
五、驗證用戶請求 && 生成HTML標籤 && 保留上次輸入內容 && 錯誤提示
Tyrion不只能夠驗證用戶請求,還能夠生成自動建立HTML標籤而且能夠保留用戶上次輸入的內容。在HTML模板中調用Form類對象的字段便可,如(Tornado示例):
from Tyrion.Forms import Form from Tyrion.Fields import StringField from Tyrion.Fields import EmailField class LoginForm(Form): username = StringField(error={'required': '用戶名不能爲空'}) password = StringField(error={'required': '密碼不能爲空'}) email = EmailField(error={'required': '郵箱不能爲空', 'invalid': '郵箱格式錯誤'})
class LoginHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): form = LoginForm(self) self.render('login.html', form=form) def post(self, *args, **kwargs): form = LoginForm(self) print(form.is_valid()) print(form.error_dict) print(form.value_dict) self.render('login.html', form=form)
<form method="post" action="/login.html"> <div> <!-- Form建立的標籤 --> {% raw form.username %} <!-- 錯誤信息 --> <span>{{form.error_dict.get('username',"")}}</span> </div> <div> {% raw form.password %} <span>{{form.error_dict.get('password',"")}}</span> </div> <div> {% raw form.email %} <span>{{form.error_dict.get('email',"")}}</span> </div> <input type="submit" value="提交"/> </form>
注意: HTML模板中的轉義
示例02:源碼下載(含有Tornado、Django、Flask、Bottle)
六、Form字段類型
Form的字段用於指定從請求中獲取的數據類型以及格式,以此來驗證用戶輸入的內容。
from Tyrion.Forms import Form from Tyrion.Fields import StringField from Tyrion.Fields import EmailField class LoginForm(Form): username = StringField(error={'required': '用戶名不能爲空'}) password = StringField(error={'required': '密碼不能爲空'}) email = EmailField(error={'required': '郵箱不能爲空', 'invalid': '郵箱格式錯誤'})
以上代碼表示此Form類能夠用於驗證用戶輸入的內容,而且 username和password必須不能爲空,email必須不能爲空而且必須是郵箱格式。
目前支持全部字段:
StringField """ 要求必須是字符串,即:正則^.*$ 參數: required 布爾值,是否容許爲空 max_length 整數,限制用戶輸入內容最大長度 min_length 整數,限制用戶輸入內容最小長度 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'invalid': '格式錯誤時的錯誤提示', 'max_length': '最大長度爲10', 'min_length': '最小長度爲1', } widget 定製生成的HTML插件(默認InputText) """ EmailField """ 要求必須是郵箱格式的字符串 參數: required 布爾值,是否容許爲空 max_length 整數,限制用戶輸入內容最大長度 min_length 整數,限制用戶輸入內容最小長度 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'invalid': '格式錯誤時的錯誤提示', 'max_length': '最大長度爲10', 'min_length': '最小長度爲1', } widget 定製生成的HTML插件(默認InputText) """ IPField """ 要求必須是IP格式 參數: required 布爾值,是否容許爲空 max_length 整數,限制用戶輸入內容最大長度 min_length 整數,限制用戶輸入內容最小長度 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'invalid': '格式錯誤時的錯誤提示', 'max_length': '最大長度爲10', 'min_length': '最小長度爲1', } widget 定製生成的HTML插件(默認InputText) """ IntegerField """ 要求必須整數格式 參數: required 布爾值,是否容許爲空 max_value 整數,限制用戶輸入數字最大值 min_value 整數,限制用戶輸入數字最小值 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'invalid': '格式錯誤時的錯誤提示', 'max_value': '最大值爲10', 'max_value': '最小值度爲1', } widget 定製生成的HTML插件(默認InputText) """ FloatField """ 要求必須小數格式 參數: required 布爾值,是否容許爲空 max_value 整數,限制用戶輸入數字最大值 min_value 整數,限制用戶輸入數字最小值 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'invalid': '格式錯誤時的錯誤提示', 'max_value': '最大值爲10', 'max_value': '最小值度爲1', } widget 定製生成的HTML插件(默認InputText) """ StringListField """ 用於獲取請求中的多個值,且保證每個元素是字符串,即:正則^.*$ 如:checkbox或selct多選時,會提交多個值,用此字段能夠將用戶提交的值保存至列表 參數: required 布爾值,是否容許爲空 ele_max_length 整數,限制用戶輸入的每一個元素內容最大長度 ele_min_length 整數,限制用戶輸入的每一個元素內容最小長度 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'element': '列表中的元素必須是字符串', 'ele_max_length': '最大長度爲10', 'ele_min_length': '最小長度爲1', } widget 定製生成的HTML插件(默認InputMultiCheckBox,即:checkbox) """ IntegerListField """ 用於獲取請求中的多個值,且保證每個元素是整數 如:checkbox或selct多選時,會提交多個值,用此字段能夠將用戶提交的值保存至列表 參數: required 布爾值,是否容許爲空 ele_max_value 整數,限制用戶輸入的每一個元素內容最大長度 ele_min_value 整數,限制用戶輸入的每一個元素內容最小長度 error 字典,自定義錯誤提示,如:{ 'required': '值爲空時的錯誤提示', 'element': '列表中的元素必須是數字', 'ele_max_value': '最大值爲x', 'ele_min_value': '最小值爲x', } widget 定製生成的HTML插件(默認InputMultiCheckBox,即:checkbox) """
七、Form字段widget參數:HTML插件
HTML插件用於指定當前字段在生成HTML時表現的種類和屬性,經過指定此參數從而實現定製頁面上生成的HTML標籤
from Tyrion.Forms import Form from Tyrion.Fields import StringField from Tyrion.Fields import EmailField from Tyrion.Widget import InputPassword class LoginForm(Form): password = StringField(error={'required': '密碼不能爲空'},widget=InputPassword())
上述LoginForm的password字段要求用戶輸入必須是字符串類型,而且指定生成HTML標籤時會建立爲<input type='password' > 標籤
目前支持全部插件:
InputText """ 設置Form對應字段在HTML中生成input type='text' 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1', 'placeholder': 'username'} """ InputEmail """ 設置Form對應字段在HTML中生成input type='email' 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1', 'placeholder': 'username'} """ InputPassword """ 設置Form對應字段在HTML中生成input type='password' 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1', 'placeholder': 'username'} """ TextArea """ 設置Form對應字段在HTML中生成 textarea 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1'} value 字符串,用於設置textarea標籤中默認顯示的內容 """ InputRadio """ 設置Form對應字段在HTML中生成一系列 input type='radio' 標籤(選擇時互斥) 參數: attr 字典,生成的HTML屬性,如:{'class': 'c1'} text_value_list 列表,生成的多個radio標籤的內容和值,如:[ {'value':1, 'text': '男'}, {'value':2, 'text': '女'}, ] checked_value 整數或字符串,默認被選中的標籤的value的值 示例: from Tyrion.Forms import Form from Tyrion.Fields import IntegerField from Tyrion.Widget import InputRadio class LoginForm(Form): favor = IntegerField(error={'required': '愛好不能爲空'}, widget=InputRadio(attr={'class': 'c1'}, text_value_list=[ {'value': 1, 'text': '男'}, {'value': 2, 'text': '女'}, ], checked_value=2 ) ) 上述favor字段生成的HTML標籤爲: <div> <span> <input class='c1' type="radio" name="gender" value="1"> </span> <span>男</span> </div> <div> <span> <input class='c1' type="radio" name="gender" value="2" checked='checked'> </span> <span>女</span> </div> """ InputSingleCheckBox """ 設置Form對應字段在HTML中生成 input type='checkbox' 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1'} """ InputMultiCheckBox """ 設置Form對應字段在HTML中生成一系列 input type='checkbox' 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1'} text_value_list 列表,生成的多個checkbox標籤的內容和值,如:[ {'value':1, 'text': '籃球'}, {'value':2, 'text': '足球'}, {'value':3, 'text': '乒乓球'}, {'value':4, 'text': '羽毛球'}, ] checked_value_list 列表,默認選中的標籤對應的value, 如:[1,3] """ SingleSelect """ 設置Form對應字段在HTML中生成 單選select 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1'} text_value_list 列表,用於指定select標籤中的option,如:[ {'value':1, 'text': '北京'}, {'value':2, 'text': '上海'}, {'value':3, 'text': '廣州'}, {'value':4, 'text': '重慶'}, ] selected_value 數字或字符串,默認被選中選項對應的值,如: 3 """ MultiSelect """ 設置Form對應字段在HTML中生成 多選select 標籤 參數: attr 字典,指定生成標籤的屬性,如: attr = {'class': 'c1'} text_value_list 列表,用於指定select標籤中的option,如:[ {'value':1, 'text': '籃球'}, {'value':2, 'text': '足球'}, {'value':3, 'text': '乒乓球'}, {'value':4, 'text': '羽毛球'}, ] selected_value_list 列表,默認被選中選項對應的值,如:[2,3,4] """
八、動態初始化默認值
因爲Form能夠用於生成HTML標籤,若是想要在建立標籤的同時再爲其設置默認值有兩種方式:
class InitValueForm(Form): username = StringField(error={'required': '用戶名不能爲空'}) age = IntegerField(max_value=500, min_value=0, error={'required': '年齡不能爲空', 'invalid': '年齡必須爲數字', 'min_value': '年齡不能小於0', 'max_value': '年齡不能大於500'}) city = IntegerField(error={'required': '年齡不能爲空', 'invalid': '年齡必須爲數字'}, widget=SingleSelect(text_value_list=[{'value': 1, 'text': '上海'}, {'value': 2, 'text': '北京'}, {'value': 3, 'text': '廣州'}]) ) gender = IntegerField(error={'required': '請選擇性別', 'invalid': '性別必須爲數字'}, widget=InputRadio(text_value_list=[{'value': 1, 'text': '男', }, {'value': 2, 'text': '女', }], checked_value=2)) protocol = IntegerField(error={'required': '請選擇協議', 'invalid': '協議格式錯誤'}, widget=InputSingleCheckBox(attr={'value': 1})) favor_int_val = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ])) favor_str_val = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ])) select_str_val = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ])) select_int_val = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ]))
class InitValueHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): form = InitValueForm(self) init_dict = { 'username': 'seven', 'age': 18, 'city': 2, 'gender': 2, 'protocol': 1, 'favor_int_val': [1, 3], 'favor_str_val': ['1', '3'], 'select_int_val': [1, 3], 'select_str_val': ['1', '3'] } # 初始化操做,設置Form類中默認值以及默認選項 form.init_field_value(init_dict) self.render('init_value.html', form=form)
九、更多示例
示例源碼下載:猛擊這裏
a. 基本使用
class RegisterForm(Form): username = StringField(max_length=32, min_length=6, error={'required': '用戶名不能爲空', 'min_length': '用戶名不能少於6位', 'max_length': '用戶名不能超過32位'}) password = StringField(max_length=32, min_length=6, error={'required': '密碼不能爲空'}, widget=InputPassword()) gender = IntegerField(error={'required': '請選擇性別', 'invalid': '性別必須爲數字'}, widget=InputRadio(text_value_list=[{'value': 1, 'text': '男', }, {'value': 2, 'text': '女', }], checked_value=2)) age = IntegerField(max_value=500, min_value=0, error={'required': '年齡不能爲空', 'invalid': '年齡必須爲數字', 'min_value': '年齡不能小於0', 'max_value': '年齡不能大於500'}) email = EmailField(error={'required': '郵箱不能爲空', 'invalid': '郵箱格式錯誤'}) city = IntegerField(error={'required': '城市選項不能爲空', 'invalid': '城市選項必須爲數字'}, widget=SingleSelect(text_value_list=[{'value': 1, 'text': '上海'}, {'value': 2, 'text': '北京'}, {'value': 3, 'text': '廣州'}]) ) protocol = IntegerField(error={'required': '請選擇協議', 'invalid': '協議格式錯誤'}, widget=InputSingleCheckBox(attr={'value': 1})) memo = StringField(required=False, max_length=150, error={'invalid': '備註格式錯誤', 'max_length': '備註最大長度爲150字'}, widget=TextArea())
b. 多選checkbox
class MultiCheckBoxForm(Form): favor_str_val = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ])) favor_str_val_default = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ], checked_value_list=['1', '4'])) favor_int_val = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ])) favor_int_val_default = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=InputMultiCheckBox(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ], checked_value_list=[2, ]))
c、多選select
class MultiSelectForm(Form): select_str_val = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ])) select_str_val_default = StringListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': '1', 'text': '籃球', }, {'value': '2', 'text': '足球', }, {'value': '3', 'text': '乒乓球', }, {'value': '4', 'text': '羽毛球'}, ], selected_value_list=['1', '3'])) select_int_val = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ])) select_int_val_default = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ], selected_value_list=[2]))
d. 動態select選項
class DynamicSelectForm(Form): city = IntegerField(error={'required': '年齡不能爲空', 'invalid': '年齡必須爲數字'}, widget=SingleSelect(text_value_list=[{'value': 1, 'text': '上海'}, {'value': 2, 'text': '北京'}, {'value': 3, 'text': '廣州'}]) ) multi_favor = IntegerListField(error={'required': '請選擇愛好', 'invalid': '選擇愛好格式錯誤'}, widget=MultiSelect(text_value_list=[{'value': 1, 'text': '籃球', }, {'value': 2, 'text': '足球', }, {'value': 3, 'text': '乒乓球', }, {'value': 4, 'text': '羽毛球'}, ])) def __init__(self, *args, **kwargs): super(DynamicSelectForm, self).__init__(*args, **kwargs) # 獲取數據庫中的最新數據並顯示在頁面上(每次建立對象都執行一次數據庫操做來獲取最新數據) self.city.widget.text_value_list = [{'value': 1, 'text': '上海'}, {'value': 2, 'text': '北京'}, {'value': 3, 'text': '南京'}, {'value': 4, 'text': '廣州'}] self.multi_favor.widget.text_value_list = [{'value': 1, 'text': '籃球'}, {'value': 2, 'text': '足球'}, {'value': 3, 'text': '乒乓球'}, {'value': 4, 'text': '羽毛球'}, {'value': 5, 'text': '玻璃球'}]
開源組件持續更新中,如您在使用過程當中遇到任何問題,請留言,我將盡快回復!!!
Tyrion技術交流QQ羣:564068039
Tyrion技術交流QQ羣:564068039
Tyrion技術交流QQ羣:564068039
重要的事情說三遍....
...
......
.........
............
.................