1. 程式人生 > >django 的form登錄 註冊

django 的form登錄 註冊

class span length 插件 特殊 登錄 for 輸入 get

  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登錄 註冊