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: