Python學習---DjangoForm的總結大全
阿新 • • 發佈:2018-08-03
== hid radi 獲取 port logs .text time ()
DjangoForm基礎知識總結
1、Form是什麽東西? 用於驗證用戶請求數據合法性的一個組件 2、 Django的Form的實現步驟: a. 創建一個驗證用戶請求的模板 from django import forms class MyForm(forms.Form): user = forms.CharField(...) # input type=‘text‘ email = forms.EmailField(...) # input type=‘email‘ pwd = forms.PaswordField(...) # input type=‘password‘ obj = MyForm(); obj.is_valid(); ------>執行上面的每一個規則,返回一個結果 b.創建模板的3個重要元素: 類 :模版,到底驗證幾個 字段:用於驗證用戶某個字段 插件:user = forms.CharField(..,widget=Input框),告訴Django,我生成什麽樣式的標簽[自己指定] 註意:Django默認的樣式是 widget = TextInput PS: 類型轉換【前臺提交的內容都是字符串,如果我們使用了IntegerField,此時數據類型就是數字類型啦】 ----------- class DetailForm(DForms.Form): user1 = fields.CharField() user2 = fields.CharField(widget=widgets.TextInput(attrs={‘class‘: ‘c1‘,‘placeholder‘: ‘用戶名‘})) user3 = fields.ChoiceField(choices=[(1, ‘SH‘), (2, ‘BJ‘), ]) user5 = fields.CharField(widget=widgets.Select(choices=[(1, ‘SH‘), (2, ‘BJ‘), ])) 字段(默認插件): CharField IntegerField IP,Email,URL,Slug ChoiceField(簡寫) ==> CharField+插件[和簡寫的等價] MultipleChoiceField RegexField 參數: required=True, 是否必填 initial="hello world", validators=[RegexValidator(r‘^[0-9]+$‘, ‘11111‘,code=‘f1‘), RegexValidator(r‘^159[0-9]+$‘, ‘2222‘,code=‘f2‘)], error_messages={‘required‘: ‘不能為空‘,‘f1‘: ‘geshicuowu‘,‘f2‘: ‘kajdlfkjasldf‘,‘max_length‘: ‘taichangla‘}, choices=[(),(),()] # 指定插件 widget = TextInput(Input) NumberInput(TextInput) EmailInput(TextInput) URLInput(TextInput) PasswordInput(TextInput) HiddenInput(TextInput) Textarea(Widget) DateInput(DateTimeBaseInput) DateTimeInput(DateTimeBaseInput) TimeInput(DateTimeBaseInput) CheckboxInput Select NullBooleanSelect SelectMultiple RadioSelect CheckboxSelectMultiple FileInput ClearableFileInput MultipleHiddenInput SplitDateTimeWidget SplitHiddenDateTimeWidget SelectDateWidget 插件用法: user1 = fields.CharField() user2 = fields.CharField(widget=widgets.TextInput) user3 = fields.CharField(widget=widgets.TextInput(attrs={})) 特殊的: 單值: user4 = fields.CharField(widget=widgets.Select(attrs={},choices=[(),(),()])) user4 = fields.ChoiceField(choices=[(),(),()],widget=widgets.Select) # 多值 user5 = fields.CharField(widget=widgets.MutipleSelect(attrs={},choices=[(),(),()])) # "[1,2,3]" user5 = fields.MultipleChoiceField(widget=widgets.MutipleSelect(attrs={},choices=[(),(),()])) # [1,2,3,4] b. 獲取用戶請求,進行驗證: - is_valid() - clean() - errors c. 表單提交 Form提交: errors.字段.0 Ajax提交: errors.as_json() errors.as_data() {‘user’: [Django對象(),]}
DjangoForm應用總結及常用插件
DjangoForm應用總結
1、form表單驗證的組件
2、類、字段、插件
3、ChoiceField
4、默認值(新URL編輯)
5、重新構造方法來實現動態獲取數據庫中的數據
常用插件
# 單radio,值為字符串 # user = fields.CharField( # initial=2, # widget=widgets.RadioSelect(choices=((1,‘上海‘),(2,‘北京‘),)) # ) # 單radio,值為字符串 # user = fields.ChoiceField( # choices=((1, ‘上海‘), (2, ‘北京‘),), # initial=2, # widget=widgets.RadioSelect # ) # 單select,值為字符串 # user = fields.CharField( # initial=2, # widget=widgets.Select(choices=((1,‘上海‘),(2,‘北京‘),)) # ) # 單select,值為字符串 # user = fields.ChoiceField( # choices=((1, ‘上海‘), (2, ‘北京‘),), # initial=2, # widget=widgets.Select # ) # 多選select,值為列表 # user = fields.MultipleChoiceField( # choices=((1,‘上海‘),(2,‘北京‘),), # initial=[1,], # widget=widgets.SelectMultiple # ) # 單checkbox # user = fields.CharField( # widget=widgets.CheckboxInput() # ) # 多選checkbox,值為列表 # user = fields.MultipleChoiceField( # initial=[2, ], # choices=((1, ‘上海‘), (2, ‘北京‘),), # widget=widgets.CheckboxSelectMultiple # )
註意區別input框的EmailField:
Python學習---DjangoForm的總結大全