Django:登录页面卡在记录经过身份验证的用户身上

时间:2013-12-07 11:02:23

标签: django authentication login admin

我有一个自定义登录页面,这里是视图:

def login(request):
    email = ''
    password = ''
    state = "Please login below: "

    form = AuthenticationForm(data=(request.POST or None))

    if form.is_valid():
        # Since the USERNAME_FIELD in custom-user is the email, that is what
        # we expect as input to the username field of this form
        email = form.cleaned_data['username']
        password = form.cleaned_data['password']

        user = authenticate(username=email,password=password)

        if user is not None and user.is_active:
            login(request, user)
            state = "You have been logged in"
        else:
            state = "Invalid login credentials"

    t = loader.get_template('login.html')
    c = RequestContext(request, {'state': state, 'form': form})
    return HttpResponse(t.render(c))

该视图正在运行,因为当我输入用户名而不是电子邮件时,会引发错误字段('创建此用户需要电子邮件')。但是当我输入正确的电子邮件和密码时,它会给我一个错误,即login()只接受一个参数。虽然我尝试传递登录1参数而不是2(登录(请求)vs登录(请求,用户)),页面不会重定向或执行任何操作。它显示它正在加载,但没有任何反应:/

以下是我的用户模型:

class CustomUserManager(BaseUserManager):
        def create_user(self, email, first_name, last_name, password=None, 
                                                      **extra_fields):
                '''
                Create a CustomUser with email, name, password and other extra fields
                '''
                now = timezone.now()
                if not email:
                        raise ValueError('The email is required to create this user')
                email = CustomUserManager.normalize_email(email)
                cuser = self.model(email=email, first_name=first_name,
                                                        last_name=last_name, is_staff=False, 
                            is_active=True, is_superuser=False,
                                                        date_joined=now, last_login=now,)
                cuser.set_password(password)
                cuser.save(using=self._db)
                return cuser

        def create_superuser(self, email, first_name, last_name, password=None, 
                                                  **extra_fields):
                u = self.create_user(email, first_name, last_name, password, 
                                                  **extra_fields)
                u.is_staff = True
                u.is_active = True
                u.is_superuser = True
                u.save(using=self._db)

                return u

class CustomUser(AbstractBaseUser, PermissionsMixin):
        '''
        Class implementing a custom user model. Includes basic django admin
        permissions and can be used as a skeleton for other models. 

        Email is the unique identifier. Email, password and name are required
        '''
        email = models.EmailField(_('email'), max_length=254, unique=True,
                                                                validators=[validators.validate_email])
        username = models.CharField(_('username'), max_length=30, blank=True)
        first_name = models.CharField(_('first name'), max_length=45)
        last_name = models.CharField(_('last name'), max_length=45)
        is_staff = models.BooleanField(_('staff status'), default=False,
                                help_text=_('Determines if user can access the admin site'))
        is_active = models.BooleanField(_('active'), default=True)
        date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

        objects = CustomUserManager()

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

        def get_full_name(self):
                '''
                Returns the user's full name. This is the first name + last name
                '''
                full_name = "%s %s" % (self.first_name, self.last_name)
                return full_name.strip()

        def get_short_name(self):
                '''
                Returns a short name for the user. This will just be the first name
                '''
                return self.first_name.strip()

我真的很感谢帮助解决这个问题!

提前多多感谢!

1 个答案:

答案 0 :(得分:2)

您的视图称为login,因此当您尝试调用登录功能时,它会调用您的视图而不是内置视图。将您的观点称为其他内容。