我的问题是,即使我输入2个不同的密码也不会显示错误消息,但是如果我输入的密码少于6个字符,则会显示错误消息
class RegistrationForm(forms.Form):
email = forms.EmailField()
password = forms.CharField(min_length= 6, widget=forms.PasswordInput())
confirm_password = forms.CharField(min_length= 6, widget=forms.PasswordInput())
registration_date = forms.DateField()
def clean(self):
password = self.cleaned_data.get('password')
confirm_password = self.cleaned_data.get('confirm_password')
if password != confirm_password:
raise ValidationError("Password error")
这是我的观点:
class Registration(FormView):
template_name = 'accounts/registration.html'
form_class = RegistrationForm
success_url = reverse_lazy('accounts:index')
def form_valid(self, form):
# my stuff...
即使两个密码不同,而且不仅密码少于6个字符,我如何强制具有这种“弹出窗口”?
答案 0 :(得分:0)
[更新]:问题似乎出在HTML模板中。 OP要做的就是将{{ form.errors }}
放在模板中,以便显示ValidationError
。
def clean(self):
cleaned_data = super().clean() # call the super clean() method first
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password and confirm_password:
if password != confirm_password:
raise ValidationError("Password error")