不能使用真实用户,但凭证在django中是正确的

时间:2014-11-06 23:54:14

标签: django django-forms django-views django-authentication

我有以下登录表单:

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

    def clean(self):
        cleaned_data = super(LoginForm, self).clean()
        email = cleaned_data.get('email')
        password = cleaned_data.get('password')
        if email and password:
            try:
                user = User.objects.get(email=email)
                if not user.check_password(password):
                    raise forms.ValidationError('Credentials do not match.')
            except User.DoesNotExist:
                raise forms.ValidationError('Customer does not exist.')
        return cleaned_data

这是我的观看代码:

if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            remember_session = form.cleaned_data['remember_session']
            if remember_session:
                request.session.set_expiry(0)
            user = authenticate(username=form.cleaned_data['email'], password=form.cleaned_data['password'])
            print user
            if user:
                login(request, user)
                return redirect(next)
            else:
                print 'something went wrong'

打印:

  

用户:无

     

出了问题

如何通过表单中的check_password但却找不到真实的用户?

1 个答案:

答案 0 :(得分:0)

问题是我正在更改用户电子邮件,但是根据用户的用户名(即电子邮件)登录。这导致了这种差异。