>[success] # 讲解 >[danger] ##### 常用的引入 ~~~ 1.from django.forms import Form 2.from django.forms import fields 3.from django.forms import widgets 4.from django.core.validators import RegexValidator 5.from cm import models 6.from django.core.exceptions import ValidationError ~~~ * 翻译 ~~~ 1. 继承Form 实例库 2. 继承form 验证字段 3. 使用input 插件 4. 正则表达式验证 5. 数据库的引入 6. 返回的错误提示 ~~~ >[danger] ##### 单个字段使用的实例 ~~~ username = fields.CharField( max_length=32, widget=widgets.TextInput(attrs={"class":"username form-control"}), validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$','4到16位(字母,数字,下划线,减号)')], error_messages={"required":"不能为空"} ) ~~~ >[danger] ##### clean_字段 用法 ~~~ def clean_email(self): v = self.cleaned_data['email'] user_obj = models.UserInfo.objects.filter(email=v).count() if user_obj: raise ValidationError("邮箱存在") return v ~~~ >[danger] ##### clean 用法 ~~~ def clean(self): value_dict = self.cleaned_data password = value_dict.get("password") confirm_password = value_dict.get("confirm_password") if password != confirm_password: raise ValidationError("密码不一致") return value_dict ~~~ >[success] # 整体代码 ~~~ from django.forms import Form from django.forms import fields from django.forms import widgets from django.core.validators import RegexValidator from cm import models from django.core.exceptions import ValidationError class RegisterForm(Form): def __init__(self,request,*args,**kwargs): self.request = request super(RegisterForm,self).__init__(*args,**kwargs) username = fields.CharField( max_length=32, widget=widgets.TextInput(attrs={"class":"username form-control"}), validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$','4到16位(字母,数字,下划线,减号)')], error_messages={"required":"不能为空"} ) nickname = fields.CharField( max_length=32, widget=widgets.TextInput(attrs={"class":"nickname form-control"}), validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$','4到16位(字母,数字,下划线,减号)')], error_messages={"required":"不能为空"} ) password = fields.CharField( max_length=64, widget = widgets.TextInput(attrs={"class":"password form-control"}), validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$', '4到16位(字母,数字,下划线,减号)')], error_messages={"required": "不能为空"} ) confirm_password = fields.CharField( max_length=64, widget=widgets.TextInput(attrs={"class":"confirm_password form-control"}), validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$', '4到16位(字母,数字,下划线,减号)')], error_messages={"required": "不能为空"} ) email = fields.EmailField( error_messages={"required":"不能为空", "invalid": "邮箱格式错误"}, widget=widgets.TextInput(attrs={"class":"nickname form-control"}) ) check_code = fields.CharField( max_length=4, error_messages={"required": "验证码不能为空", "max_length": "请输入正确验证码"} ) def clean_username(self): v = self.cleaned_data['username'] user_obj = models.UserInfo.objects.filter(username=v).count() if user_obj: raise ValidationError("昵称已被注册") return v def clean_email(self): v = self.cleaned_data['email'] user_obj = models.UserInfo.objects.filter(email=v).count() if user_obj: raise ValidationError("邮箱存在") return v def clean_check_code(self): v = self.cleaned_data['check_code'] if self.request.session.get('CheckCode').upper() != v.upper(): raise ValidationError("验证码错误") return v def clean(self): value_dict = self.cleaned_data password = value_dict.get("password") confirm_password = value_dict.get("confirm_password") if password != confirm_password: raise ValidationError("密码不一致") return value_dict ~~~