django 的form登录 注册

Posted 派神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 的form登录 注册相关的知识,希望对你有一定的参考价值。

  1 #!/usr/bin/env python
  2 # -*- coding: utf8 -*-
  3 #__Author: "Skiler Hao"
  4 #date: 2017/3/30 15:40
  5 from django.core.exceptions import ValidationError  #抛出异常
  6 from django import forms 
  7 from django.forms import fields
  8 from django.forms import widgets   #内部有插件 用于密码什么二次验证
  9 from django.core.validators import RegexValidator  #定义正则表达式
 10 from respository import models
 11 
 12 
 13 class RegisterForm(forms.Form):
 14     username = fields.CharField(
 15         required=True,
 16         widget=widgets.TextInput(attrs={class: "form-control",placeholder: 用户名为8-12个字符}),
 17         min_length=6,
 18         max_length=12,
 19         strip=True,
 20         error_messages={required: 标题不能为空,
 21                         min_length: 用户名最少为6个字符,
 22                         max_length: 用户名最不超过为20个字符},
 23     )
 24     email = fields.EmailField(
 25         required=True,
 26         widget=widgets.TextInput(attrs={class: "form-control",placeholder: 请输入邮箱}),
 27         strip=True,
 28         error_messages={required: 邮箱不能为空,
 29                         invalid:请输入正确的邮箱格式},
 30     )
 31     pwd = fields.CharField(
 32         widget=widgets.PasswordInput(attrs={class: "form-control",placeholder: 请输入密码,必须包含数字,字母,特殊字符},render_value=True),
 33         required=True,
 34         min_length=6,
 35         max_length=12,
 36         strip=True,
 37         validators=[
 38             # 下面的正则内容一目了然,我就不注释了
 39             RegexValidator(r((?=.*\d))^.{6,12}$, 必须包含数字),
 40             RegexValidator(r((?=.*[a-zA-Z]))^.{6,12}$, 必须包含字母),
 41             RegexValidator(r((?=.*[^a-zA-Z0-9]))^.{6,12}$, 必须包含特殊字符),
 42             RegexValidator(r^.(\S){6,10}$, 密码不能包含空白字符),
 43         ], #用于对密码的正则验证
 44         error_messages={required: 密码不能为空!,
 45                         min_length: 密码最少为6个字符,
 46                         max_length: 密码最多不超过为12个字符!,},
 47     )
 48     pwd_again = fields.CharField(
 49         #render_value会对于PasswordInput,错误是否清空密码输入框内容,默认为清除,我改为不清楚
 50         widget=widgets.PasswordInput(attrs={class: "form-control",placeholder: 请再次输入密码!},render_value=True),
 51         required=True,
 52         strip=True,
 53         error_messages={required: 请再次输入密码!,}
 54 
 55     )
 56 
 57     def clean_username(self):
 58         # 对username的扩展验证,查找用户是否已经存在
 59         username = self.cleaned_data.get(username)
 60         users = models.User.objects.filter(username=username).count()
 61         if users:
 62             raise ValidationError(用户已经存在!)
 63         return username
 64 
 65     def clean_email(self):
 66         # 对email的扩展验证,查找用户是否已经存在
 67         email = self.cleaned_data.get(email)
 68         email_count = models.User.objects.filter(email=email).count() #从数据库中查找是否用户已经存在
 69         if email_count:
 70             raise ValidationError(该邮箱已经注册!)
 71         return email
 72 
 73     def _clean_new_password2(self): #查看两次密码是否一致
 74         password1 = self.cleaned_data.get(pwd)
 75         password2 = self.cleaned_data.get(pwd_again)
 76         if password1 and password2:
 77             if password1 != password2:
 78                 # self.error_dict[‘pwd_again‘] = ‘两次密码不匹配‘
 79                 raise ValidationError(两次密码不匹配!)
 80 
 81     def clean(self):
 82         #是基于form对象的验证,字段全部验证通过会调用clean函数进行验证
 83         self._clean_new_password2() #简单的调用而已
 84 
 85 
 86 class loginForm(forms.Form):
 87     username = fields.CharField(
 88         required=True,
 89         widget=widgets.TextInput(attrs={class: "form-control",placeholder: 请输入用户名}),
 90         min_length=6,
 91         max_length=12,
 92         strip=True,
 93         error_messages={required: 用户名不能为空,}
 94     )
 95 
 96     pwd = fields.CharField(
 97         widget=widgets.PasswordInput(attrs={class: "form-control",placeholder: 请输入密码}),
 98         required=True,
 99         min_length=6,
100         max_length=12,
101         strip=True,
102         error_messages={required: 密码不能为空!,}
103     )
104 
105     def clean(self):
106         username = self.cleaned_data.get(username)
107         pwd = self.cleaned_data.get(pwd)
108         user = models.User.objects.filter(username=username).first()
109         if username and pwd:
110             if not user :
111 
112                 # self.error_dict[‘pwd_again‘] = ‘两次密码不匹配‘
113                 raise ValidationError(用户名不存在!)
114             elif pwd != user.password:
115                 raise ValidationError(密码不正确!)
116 
117 forms.py

 

以上是关于django 的form登录 注册的主要内容,如果未能解决你的问题,请参考以下文章

Django基于Form之登录和注册

Django框架form表单配合ajax注册

Django - 基于forms组件和Ajax实现注册登录 - FileField字段 - Media配置

41.登录注册小案例实现(使用Django中的form表单来进行用户输入数据的校验;文末附源码)

41.登录注册小案例实现(使用Django中的form表单来进行用户输入数据的校验;文末附源码)

django项目 crm登录 注册