Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin does not login properly with custom User model

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?

like image 981
Sanketh Katta Avatar asked Mar 03 '13 01:03

Sanketh Katta


1 Answers

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
like image 70
alioguzhan Avatar answered Sep 19 '22 21:09

alioguzhan