首页 > 解决方案 > 登录验证 - 错误消息 - 始终相同

问题描述

我使用最新版本的 Django (2.05)

我创建了一个自定义用户模型:

class User(AbstractBaseUser, MermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=False)  # can't login


class ULoginForm(AuthenticationForm):

    # we declared this way to override the AuthenticationForm fields
    username = UsernameField(max_length=254, widget=TextWidget(attrs={'autofocus': True, 'placeholder': 'Email*'}))
    password = forms.CharField(strip=False, widget=PasswordWidget(attrs={'placeholder': 'Password*'}))

首先,如果凭据正确,我可以登录,问题是如果出现验证错误,我会收到相同的消息:

Please enter a correct email and password. Note that both fields may be case-sensitive.

如果用户/密码正确,则帐户未激活,则消息应该不同,account inactive以了解需要确认电子邮件的用户。

之后,在 Django AutentificationForm 中进一步调查它是以下代码:

def clean:

      if username is not None and password:
                self.user_cache = authenticate(self.request, username=username, password=password)

                if self.user_cache is None:
                    raise forms.ValidationError(
                        self.error_messages['invalid_login'],

self.user_cache总是返回 None 这似乎是问题所在;

标签: djangodjango-authentication

解决方案


推荐阅读