django - 登录验证 - 错误消息 - 始终相同
问题描述
我使用最新版本的 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 这似乎是问题所在;
解决方案
推荐阅读
- bash - 为什么bash命令“set”列出不同数量的变量
- excel - 遍历数组以从公式中查找结果
- python - 在 python 中将生成器转换为 Numpy 数组
- python - 如何将 Plotly 升级到最新版本?
- python - 无法使用导出导出 Yolov5 模型
- ansible - Ansible Inventory:如何从不在某些其他组中的主机构建组
- javascript - Oracle APEX 警告消息
- r - 如何在函数中使用包含多个变量的选择?
- reactjs - 类型对象上不存在属性“过滤器”
- bootstrap-5 - 未捕获的类型错误:z.fn.dropdown 未定义