Auth组件,Forms组件

class CheckForm(forms.Form):

# 校验需求:账号不能以数字开头<br/>
usr = forms.CharField(min_length=3, max_length=10, label=&#34;账号:&#34;,<br/>
                      error_messages={<br/>
                          &#39;required&#39;: &#34;必填项&#34;,<br/>
                          &#39;min_length&#39;: &#34;最少3&#34;,<br/>
                          &#39;max_length&#39;: &#34;最多10&#34;<br/>
                      })<br/>
pwd = forms.CharField(min_length=3, max_length=10, label=&#34;密码:&#34;,<br/>
                      error_messages={<br/>
                          &#39;required&#39;: &#34;必填项&#34;,<br/>
                          &#39;min_length&#39;: &#34;最少3&#34;,<br/>
                          &#39;max_length&#39;: &#34;最多10&#34;<br/>
                      },<br/>
                      widget=forms.PasswordInput(attrs={<br/>
                          &#39;class&#39;: &#39;pwd&#39;,<br/>
                          &#39;placeholder&#39;: &#39;请输入密码&#39;<br/>
                      })<br/>
                      )<br/>
re_pwd = forms.CharField(min_length=3, max_length=10, label=&#34;确认:&#34;,<br/>
                         error_messages={<br/>
                             &#39;required&#39;: &#34;必填项&#34;,<br/>
                             &#39;min_length&#39;: &#34;最少3&#34;,<br/>
                             &#39;max_length&#39;: &#34;最多10&#34;<br/>
                         },<br/>
                         widget=forms.PasswordInput)<br/>
email = forms.EmailField(label=&#34;邮箱:&#34;,<br/>
                         error_messages={<br/>
                             &#39;invalid&#39;: &#34;格式不正确&#34;,<br/>
                             &#39;required&#39;: &#34;必填项&#34;<br/>
                         }<br/>
                         )

局部钩子:对usr进行局部钩子的校验,该方法会在usr属性校验通过后,系统调用该方法继续校验

def clean_usr(self):<br/>
    cleaned_usr = self.cleaned_data.get(&#39;usr&#39;, None)  # type: str<br/>
    # 通过正则匹配不能以数字开头<br/>
    import re<br/>
    if re.match(&#39;^[0-9]&#39;, cleaned_usr):<br/>
        from django.core.exceptions import ValidationError<br/>
        raise ValidationError(&#39;不能以数字开头&#39;)<br/>
    return cleaned_usr

全局钩子:代表校验类中的所有属性校验通过后,系统调用该方法继续校验

def clean(self):<br/>
    cleaned_pwd = self.cleaned_data.get(&#39;pwd&#39;, None)<br/>
    cleaned_re_pwd = self.cleaned_data.get(&#39;re_pwd&#39;, None)<br/>
    if cleaned_pwd != cleaned_re_pwd:<br/>
        from django.core.exceptions import ValidationError<br/>
        raise ValidationError(&#39;两次密码不一致&#39;)<br/>
    return self.cleaned_data

def register(request):

if request.method == &#34;GET&#34;:<br/>
    check_form = CheckForm()<br/>
if request.method == &#34;POST&#34;:<br/>
    check_form = CheckForm(request.POST)<br/>
    if check_form.is_valid():<br/>
        return HttpResponse(&#39;注册成功&#39;)<br/>
    else:<br/>
        print(check_form.errors.as_data)<br/>
        all_error = check_form.errors.get(&#39;__all__&#39;)<br/>
return render(request, &#39;register.html&#39;, locals())