django视图中的request.user.is_authenticated()始终返回false

时间:2015-02-20 17:30:38

标签: python django request django-views google-plus

我正在尝试使用Google Oauth2密钥,使用django-social-auth设置谷歌+登录我的网站。我在views.py中包含了2个函数defs,一个用于索引和其他配置文件。因此,在索引中尝试登录,如果有传递,则HttpResponseRedirects到配置文件页面。我停留在这一点,谷歌+登录对话框弹出,我输入用户凭据,它被接受。但是,索引不会重定向到配置文件页面。如果您看到,在下面的代码中, request.user.is_authenticated()始终返回false。在打印request.user时,我总是得到 AnonymousUser 。如果我在这里做错了或者我需要在代码中添加一些东西,请告诉我。 我的views.py目前看起来像这样。

def index(request):
    #print request.user

    if request.user.is_authenticated():
        print "user is authenticated"
        return HttpResponseRedirect('/profile')

    return render_to_response('index.html', {}, context_instance=RequestContext(request))

def profile(request):
    return render_to_response('profile.html')

设置文件具有以下相关参数:

GOOGLE_OAUTH2_CLIENT_ID = <client-id>
GOOGLE_OAUTH2_CLIENT_SECRET = <secret-key>
GOOGLE_OAUTH2_USE_UNIQUE_USER_ID = True

LOGIN_URL          = '/'
LOGIN_REDIRECT_URL = '/profile/'
LOGIN_ERROR_URL    = '/login-error/'

AUTHENTICATION_BACKENDS = (
'social_auth.backends.google.GoogleOAuthBackend',
'social_auth.backends.google.GoogleOAuth2Backend',
'social_auth.backends.google.GoogleBackend',
'social_auth.backends.OpenIDBackend',
'django.contrib.auth.backends.ModelBackend',
)

我执行此任务时遵循此google developer's page

我尝试了链接w.r.t RequestContext解决方案以及similar problem中提到的解决方案,但两者都没有成效。

0 个答案:

没有答案