0
我已經寫了乾淨的基於字段的驗證。要檢查我輸入的密碼是否匹配,請擦除這兩個字段的密碼。這可能會惹惱用戶。我想要這樣的「如果錯誤讓用戶改變了一點..不要重新鍵入整個密碼」。但是,錯誤發生後,用戶名驗證會將用戶名保留在 字段中。爲什麼發生這種情況?Django表單驗證不保留密碼與ValidationError
class signupform(ModelForm):
prmqpass=forms.CharField(widget=forms.PasswordInput())
prmqpass1=forms.CharField(widget=forms.PasswordInput())
class Meta:
model=prmqsignup
exclude=['prmqactivated']
def clean_prmqpass(self):
cd=self.cleaned_data
pwd=cd.get("prmqpass")
if len(str(pwd))<6:
raise ValidationError("Password must be at least of six characters")
if not passstrength(pwd):
raise ValidationError("For stronger security password must contain at least one uppercase,lowercase,number and special character")
return pwd
def clean_prmquname(self):
cd=self.cleaned_data
usrname=cd.get("prmquname")
if len(usrname)<5:
raise ValidationError("Username too short choose more than four characters")
if prmqsignup.objects.exclude(pk=self.instance.pk).filter(prmquname=usrname).exists():
raise ValidationError('Username "%s" is already in use.' % usrname)
return usrname
def clean(self):
cd=self.cleaned_data
pwd=cd.get("prmqpass")
pwd1=cd.get("prmqpass1")
if not pwd==pwd1:
raise ValidationError("Passwords don't match.")
return cd
爲什麼用戶名值(留在窗體中)和密碼值(閃爍後)出現錯誤。請參閱驗證邏輯是否相同。