Django用户身份验证 - 无法传递@login_required装饰器

时间:2014-07-04 03:09:26

标签: python django python-2.7

因此,我目前正尝试通过Braintree(https://github.com/Tivix/django-braintree)实施现有的付款处理申请,以供参考。似乎这个应用程序的所有内容都放在/ payments-billing /目录下,但我似乎无法进入它查看它。似乎阻止我的是一个@login_required装饰器放在视图之前,因为每当我访问目录时它都会将我发送回定义的LOGIN_URL。但是,我在LOGIN_URL上设置了一个登录功能,用于对用户进行身份验证,然后将其发送到/ payments-billing /,但它只是重定向回来。这是我的代码:

username = form.cleaned_data['username']
password = form.cleaned_data['password']

user = authenticate(username=username, password=password)
if user is not None:
    # Password verified for user
    if user.is_active:
       return redirect(self.success_url)
    else:
        return redirect('/')
else:
    return redirect('/')

显然,用户正在进行身份验证并处于活动状态,因为它在您尝试时通过了两个测试,但它始终只是将用户发送回LOGIN_URL而不是/ payments-billing /。任何人都知道这里的交易是什么?

2 个答案:

答案 0 :(得分:3)

authenticate函数不会记录用户,只会检查用户名/密码。您还必须致电django.contrib.auth.login()进行实际登录。请参阅the documentation中的示例。

答案 1 :(得分:0)

username = form.cleaned_data['username']
password = form.cleaned_data['password']

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

login(request, user)  
### Now it should get redirected to  /payments-billing/

if user is not None:
    # Password verified for user
    if user.is_active:
       return redirect(self.success_url)
    else:
        return redirect('/')
else:
    return redirect('/')