简体   繁体   中英

django - Cannot login with the user credentials

I have a custom user model and its user manager.

class CustomUserManager(BaseUserManager):
    def _create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number,
                     is_active, is_admin, is_superuser):
        """
        Creates and saves a user with given email and password
        """
        if not email:
            raise ValueError('Email must be set')

        email = self.normalize_email(email)
        now = timezone.now()

        user = self.model(
            email=email,
            username=username,
            first_name=first_name,
            last_name=last_name,
            date_of_birth=date_of_birth,
            gender=gender,
            mobile_number=mobile_number,
            date_joined=now,
            is_active=is_active,
            is_admin=is_admin,
            is_superuser=is_superuser,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
        user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, False, False, **extra_fields)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
        user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, True, True, **extra_fields)
        user.save(using=self._db)
        return user


class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), max_length=254, unique=True)
    username = models.CharField(_('user name'), max_length=254, unique=True)
    first_name = models.CharField(_('first name'), max_length=30)
    last_name = models.CharField(_('last name'), max_length=30)
    date_of_birth = models.DateField()
    gender = models.CharField(choices=GENDERTYPE, max_length=1)
    mobile_number = models.IntegerField(unique=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number']
    ...
    ...

And to process the model, I have a custom user creation forms.py

class UserCreationForm(forms.ModelForm):
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    class Meta:
        model = CustomUser
        fields = ('email', 'username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number')

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        print self.cleaned_data["password"]
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = CustomUser
        fields = (
            'email', 'username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number', 'is_active',
            'is_admin'
        )

    def clean_password(self):
        return self.initial["password"]

I can save and create a new user. Also I can see the hashed password in the admin. However when I try to login with the email and password of the new user, I am not being authenticated. I also tried to login with the username and the password , but that didn't work either.

To check if I am actually getting any password, I tried printing the self.cleaned_data["password"] in the UserCreationForm , and in the console I can see the password I gave. However, if I try to login in the admin, I not able to login. But again, with the superuser's credentials, I can login. What am I doing wrong here?

如果您尝试登录到管理站点,请确保您的新用户具有管理权限

is_admin=True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM