Saturday, July 23, 2016

Re: Cannot login with user credentials

But again, with the superuser's credentials, I can login

On Sat, Jul 23, 2016 at 12:47 PM, Robin Lery <robinlery@gmail.com> wrote:
I have a made custom user model.

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. 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. What am I doing wrong here? Please help me.

Thank you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGocC6r9zwG0a6niNM7DRtnnEOyRhnk0cV8U_tQy8nyzqg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment