0
我有一個允許用戶創建用戶帳戶的表單。我還強制密碼的最小長度爲8,如果違反,會引發錯誤。問題是,每當最小密碼長度爲8時,它不會引發錯誤。當需要最小長度的密碼時,Django無法提出錯誤
有人可以幫助我。
def Registration(request):
form = UserRegistration()
if request.method =='POST':
form = UserRegistration(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password']
)
user.is_active = True
user.save()
return render(request,'choose.html',{'form':form})
return render(request, 'register.html', {'form': form},)
我forms.py
class UserRegistration(forms.Form):
username = forms.CharField()
email = forms.EmailField(error_messages={'required':'Error Missing Field , Please Fill this Field'})
password = forms.CharField(
widget=forms.PasswordInput(render_value=False)
)
MIN_LENGTH = 8
def clean_password(self):
password = self.cleaned_data['password']
if len(password) > self.MIN_LENGTH:
raise forms.ValidationError("The new password must be at least %d characters long." % self.MIN_LENGTH)
return password
我的模板
<form method ="POST">
{% csrf_token %}
<span id="error">{{form.username.errors}}</span>
<span id="error2">{{form.email.errors}}</span>
<span id="error3">{{form.password.errors}}</span>
{{form.username}}
{{form.email}}
{{form.password}}
<input type = "submit" value= "save" id="box2"/>
</form>
並使用<在密碼長度測試 – Yossi
@Yossi謝謝貝納 – donkeyboy72
你猜對了吧:) – donkeyboy72