Django:登录表单似乎不起作用

时间:2015-01-09 05:32:09

标签: django forms

我想使用以下表单登录用户,但每次提交时都会出现表单验证错误,即使用户的电子邮件和密码是正确的:抛出错误的部分是我要检查的位置用户未将电子邮件和密码留空。当我把这个部分从代码中解脱出来但当我把它放回去时它会打破整个过程。

class LoginForm(forms.Form):
    email = forms.CharField(required=False)
    password = forms.CharField(required=False, widget=forms.PasswordInput())

    def clean_email(self):
        email = self.cleaned_data.get("email")
        password = self.cleaned_data.get("password")
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            raise forms.ValidationError("Sorry, a user with that login email does not exists")
        if not (email and password):
            raise forms.ValidationError("Sorry, the email and password provided do not match.")
        return email

    def clean_password(self):
        email = self.cleaned_data.get("email")
        password = self.cleaned_data.get("password")
        try:
            user = User.objects.get(email=email)
        except:
            user = None
        if user is not None and not user.check_password(password):
            raise forms.ValidationError("Sorry, the email and password provided do not match.")

        elif user is None:
            pass
        else:
            return password

0 个答案:

没有答案