我的自定义用户身份验证无效

时间:2017-05-11 00:31:03

标签: python django authentication django-models django-forms

我为我的Django项目创建了一个自定义用户模型,以便用户可以使用他们的电子邮件登录。但是,身份验证不起作用 这是我的代码:
的观点:

def login_view(request):
if request.method == "POST":
    form = LoginForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        user = authenticate(usename=email, password=password)
        if user:
            print(True)
            login(request, user)
            return redirect('/account/')
        else:
            print(str(password)+" "+str(email))
            print(False)
else:
    form = LoginForm()
return render(request, 'users/login.html',{'form': form})

我的后端:

def authenticate(self, username=None, password=None,):
    try:
        user = User.objects.get(email=username)
        if user.check_password(password):
            return True
    except User.DoesNotExist:
        return True

def get_user(self, user_id):
    try:
        user = User.objects.get(pk=user_id)
        if user.is_active:
            return user
        return None
    except User.DoesNotExist:
        return None

我的用户模型:

class UserManager(BaseUserManager):
def _create_user(self, first_name, last_name, username, email, is_admin, password=None, **extra_fields):
    """
    creates a User with first name, last name, username, email,  password
    """

    if not email:
        raise ValueError('The given email must be set')

    email = self.normalize_email(email)
    user = self.model(
        first_name=first_name,
        last_name=last_name,
        username=username,
        email=email,
        is_admin=is_admin,
        is_active=True,
        **extra_fields,
    )
    user.set_password(password)
    user.save(using=self._db)
    return user

def create_user(self, first_name, last_name, username, email, password, **extra_fields):
    return self._create_user(
        first_name,
        last_name,
        username,
        email,
        False,
        password,
        **extra_fields
    )

def create_superuser(self, first_name, last_name, username, email, password=None, **extra_fields):
    return self._create_user(
        first_name,
        last_name,
        username,
        email,
        True,
        password,
        **extra_fields
    )


class User(AbstractBaseUser):
first_name = models.CharField(
    max_length=256,
    unique=False,
    verbose_name='first name',
)
last_name = models.CharField(
    verbose_name='last name',
    max_length=256,
    unique=False,
)
username = models.CharField(
    verbose_name='username',
    max_length=256,
    unique=False,
)
email = models.EmailField(
    verbose_name='email',
    max_length=256,
    unique=True,
)
is_active = models.BooleanField(
    default=True,
)
 is_admin = models.BooleanField(
     default=False,
 )

objects = UserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = [
    'first_name',
    'last_name',
    'username',
]

class Meta:
    verbose_name = _('user')
    verbose_name_plural = _('users')

def get_full_name(self):
    full_name = "{} {}".format(self.first_name, self.last_name)
    return full_name.strip()

def get_short_name(self):
    return self.first_name

def __str__(self):  # __unicode__ on Python 2
    return self.email

def email_user(self, subject, message, from_email):
    send_mail(subject, message, from_email, [self.email])

def has_module_perms(self, app_label):
    "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True

def has_perm(self, perm, obj=None):
    return self.is_admin

@property
def is_staff(self):
    "Is the user a member of staff?"
    # Simplest possible answer: All admins are staff
    return self.is_admin

我的设置:

 AUTH_USER_MODEL = 'custom_user.User'

 AUTHENTICATION_BACKENDS = ('custom_user.backends.UserAuth', )

我的表格:

     email = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'registerforms', 'placeholder': 'Email'}),
    label='',
)
password = forms.CharField(
    widget=forms.PasswordInput(attrs={'class': 'registerforms', 'placeholder': 'Password'}),
    label='',
)

我没有收到错误,但它没有在

中记录用户

0 个答案:

没有答案