1. 程式人生 > >Tyrion中文文檔(含示例源碼)

Tyrion中文文檔(含示例源碼)

南京 html模板 cti 選項 dem isp 兩種 github 獲取

Tyrion是一個基於Python實現的支持多個WEB框架的Form表單驗證組件,其完美的支持Tornado、Django、Flask、Bottle Web框架。Tyrion主要有兩大重要動能:

  • 表單驗證
  • 生成HTML標簽
  • 保留上次提交內容

對於表單驗證,告別書寫重復的正則表達式對用戶提交的數據進行驗證的工作,從此解放雙手,跟著我左手右手一個慢動作...

對於生成HTML標簽,不在人工書寫html標簽,讓Tyrion幫你自動創建...

對於保留上次提交內容,由於默認表單提交後頁面刷新,原來輸入的內容會清空,Tyrion可以保留上次提交內容。

github:https://github.com/WuPeiqi/Tyrion

技術分享

使用文檔

1、下載安裝

pip install PyTyrion 

github: https://github.com/WuPeiqi/Tyrion

2、配置WEB框架種類

由於Tyrion同時支持Tornado、Django、Flask、Bottle多個WEB框架,所有在使用前需要進行指定。

        import Tyrion
        Tyrion.setup(‘tornado‘) 
        # setup的參數有:tornado(默認)、django、bottle、flask

3、創建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‘: ‘郵箱格式錯誤‘})

4、驗證用戶請求

前端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)

5、驗證用戶請求 && 生成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: 郵箱格式錯誤})
Form類 技術分享
        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)
處理請求(Tornado) 技術分享
        <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模板login.html

註意: HTML模板中的轉義

示例02:源碼下載(含有Tornado、Django、Flask、Bottle)

6、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)
    """
View Code

7、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]
        """
View Code

8、動態初始化默認值

由於Form可以用於生成HTML標簽,如果想要在創建標簽的同時再為其設置默認值有兩種方式:

  • 靜態,在插件參數中指定
  • 動態,調用Form對象的 init_field_value 方法來指定
技術分享
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: 羽毛球}, ]))
動態初始值 - Form類 技術分享
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)
動態初始值 - 處理請求的Handler(Tornado)

9、更多示例

示例源碼下載:猛擊這裏

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

重要的事情說三遍....

...

......

.........

............

.................  

Tyrion中文文檔(含示例源碼)