我想在UserCreationForm中引发错误。我已经看过有关此问题的先前问题,但对我而言不起作用。我给了现有的电子邮件,提交时,页面上没有错误显示。你能发现我的错误吗?
我如何在forms.py中写
class StudentRegisterForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = ['email', ....]
def __init__(self, *args, **kwargs):
email_attr = {'class': 'form-control', 'id': 'email-register', 'type': 'email'}
super(StudentRegisterForm, self).__init__(*args, **kwargs)
self.fields['email'].widget.attrs.update(email_attr)
# some other fields
def clean_email(self):
email = self.cleaned_data.get('email')
if CustomUser.objects.filter(email=email).exists():
raise forms.ValidationError("Email's already taken!")
return email
views.py
class StudentRegisterView(SuccessMessageMixin, CreateView):
template_name = "attendance/login-register/student_register.html"
form_class = StudentRegisterForm
success_url = "/"