Form表单验证

 1、创建Form类

Django——form表单验证(创建,内置字段,自定义异常,form表单的前端数据实时更新)

 1 from django.forms import Form
 2 from django.forms import widgets
 3 from django.forms import fields
 4 
 5 class FM(Form):
 6     user = fields.CharField(
 7         error_messages={'required':'用户名不能为空'}
 8     )
 9     pwd = fields.CharField(
10         max_length=18,
11         min_length=9,
12         error_messages={'required':'密码不能为空','max_length':'密码长度不能大于12','min_length':'密码长度不能小于6'},
13         widget=widgets.PasswordInput(attrs={'class':'c1','id':'i1'})
14     )
15     email = fields.EmailField(
16         error_messages={'required': '邮箱不能为空.', 'invalid': "邮箱格式错误"}
17     )
18     gender = fields.ChoiceField(
19         choices=((1,''),(2,''),),
20         initial = 2,     #默认值
21         # widget=widgets.Select
22         widget=widgets.RadioSelect
23     )
24     city = fields.CharField(
25         initial=2,
26         widget=widgets.Select(choices=((1,'上海'),(2,'北京'),))
27     )
28     abc = fields.MultipleChoiceField(
29         choices=((1,'a'),(2,'b'),(3,'c'),(4,'d')),
30         initial=[1,2],
31         widget = widgets.CheckboxSelectMultiple
32     )
View Code

相关文章:

  • 2021-05-29
  • 2021-10-22
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案