Django管理员无法使用自定义用户模型正确登录
我将Django 1.4.5上的一个应用程序升级到Django 1.5,刚刚完成向自定义用户模型的迁移。当我使用自己的身份验证表单和超级用户凭据(在执行Django管理员无法使用自定义用户模型正确登录,django,django-admin,django-1.5,Django,Django Admin,Django 1.5,我将Django 1.4.5上的一个应用程序升级到Django 1.5,刚刚完成向自定义用户模型的迁移。当我使用自己的身份验证表单和超级用户凭据(在执行manage.py syncdb时创建)登录到我的应用程序时,一切正常 我能够获得身份验证,如果我转到/admin,我已按预期登录。我能够导航和使用管理面板完美。但是,如果我尝试使用django admin登录表单从/admin登录到管理面板,则会出现以下错误: 请为员工帐户输入正确的电子邮件和密码。注 这两个字段可能区分大小写 我做了一些调查,
manage.py syncdb
时创建)登录到我的应用程序时,一切正常
我能够获得身份验证,如果我转到/admin
,我已按预期登录。我能够导航和使用管理面板完美。但是,如果我尝试使用django admin登录表单从/admin
登录到管理面板,则会出现以下错误:
请为员工帐户输入正确的电子邮件和密码。注
这两个字段可能区分大小写
我做了一些调查,认为这可能与ModelAdmin
有关,所以我根据文档创建了一个自定义ModelAdmin
。然而,这个问题仍然存在
你知道这是什么原因吗?你是否在
BaseUserManager
下的create\u superuser
函数中添加了以下行?可能是这样的:
class CustomUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not username:
raise ValueError('Dude you need a username.')
if not email:
raise ValueError(
'type an e-mail..')
new_user = self.model(
username=username,
email=CustomUserManager.normalize_email(email))
new_user.set_password(password)
new_user.save(using=self._db)
return new_user
def create_superuser(self, username, email, password):
new = self.create_user(
username,
email,
password=password
)
new.is_active = True
new.is_staff = True
new.is_superuser = True
new.save(using=self._db)
return new
关注:
new.is_active = True
new.is_staff = True
new.is_superuser = True
我也有同样的问题,但我发现@alix的答案非常有用。我忘记在创建超级用户()时添加
is\u active=True
所以基本上你需要专注于你是否活跃并检查你的状态
# changes the in-build User model to ours (Custom)
AUTH_USER_MODEL = 'accounts.User'
AUTHENTICATION_BACKENDS = (
# Needed to login by custom User model, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)
您在settings.py中设置了AUTH_USER_MODEL,对吗,我已经设置好了。您是否使用了
AbstractUser
或AbstractBaseUser
作为您的用户模型扩展?我使用了AbstractBaseUser
。超级用户必须等于True,这就是为什么您不能登录管理员页面django的管理员检查request.User.是否处于活动状态
和request.User.是否有员工
来登录中的用户,因此我认为他应该在列表中添加new.is\u active=True
以“关注”。当我这样做时,是is\u staff=True而不是is\u staff=False。管理员登录为我工作!
# changes the in-build User model to ours (Custom)
AUTH_USER_MODEL = 'accounts.User'
AUTHENTICATION_BACKENDS = (
# Needed to login by custom User model, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)