Django admin does not login properly with custom User model

↘锁芯ラ 提交于 2020-01-02 02:11:11

问题


I upgraded an app I had on Django 1.4.5 to Django 1.5 and just finished migrating over to a custom User model. When I login to my app, using my own authentication form, with my superuser credentials (created when doing manage.py syncdb) everything works fine.

I am able to get authenticated and if I go to /admin, I am already logged in, as expected. I am able to navigate and use the Admin panel perfectly. However, if I try to login to the admin panel from /admin, using the django admin login form, I get the error:

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

I did some investigating and thought it could have something to do with ModelAdmin, so I followed this example from the docs and created a custom ModelAdmin. However, the problem still persists.

Any ideas what could be causing this?


回答1:


Did you add following lines in to your create_superuser function which is under BaseUserManager? It might look like this:

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

Focus on:

    new.is_active = True
    new.is_staff = True
    new.is_superuser = True



回答2:


I had the same issues but I found @alix's answer so helpfully. I had forgotten to add is_active=True on create_superuser()

def create_staffuser(self, email, first_name=None, last_name=None, user_role=None, password=None):
    user = self.create_user(
        email,
        first_name=first_name,
        last_name=last_name,
        user_role=user_role,
        password=password,
        is_staff=True,
        is_active=True
    )
    return user

so basically you need to focus on is_active and check your

# 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",
)


来源:https://stackoverflow.com/questions/15181379/django-admin-does-not-login-properly-with-custom-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!