Python Django Social Auth未向数据库添加具有已保存Google凭据的新用户 脚本:

Python Django Social Auth未向数据库添加具有已保存Google凭据的新用户 脚本:,python,django,authentication,social,Python,Django,Authentication,Social,当用户试图通过谷歌登录时,如果他们还没有登录 在数据库中,它们被重定向回另一个非用户状态,而不应该这样做。如果用户试图使用现有的电子邮件地址通过谷歌登录,他们已经在数据库中,那么他们的身份验证就可以了。在请求中,即使用户通过Google进行身份验证并成功获得访问令牌和所有内容,请求仍然认为它是匿名用户 在这之前,它曾经工作过,但现在不再工作了 代码: views.py def index(request): try: # print(request.user) retur

当用户试图通过谷歌登录时,如果他们还没有登录 在数据库中,它们被重定向回另一个非用户状态,而不应该这样做。如果用户试图使用现有的电子邮件地址通过谷歌登录,他们已经在数据库中,那么他们的身份验证就可以了。在请求中,即使用户通过Google进行身份验证并成功获得访问令牌和所有内容,请求仍然认为它是匿名用户

在这之前,它曾经工作过,但现在不再工作了

代码: views.py

def index(request):
    try:
        # print(request.user) returns AnonymousUser even after authenticating
        profile = Profile.objects.get(email=request.user.email)
        return render(request, 'tablefor2/index-logged-in.html')
    except:
        return render(request, 'tablefor2/index-logged-out.html')
HTML


谢谢

修正了!发现这是因为我的Profile对象有一个is_活动字段,它被设置为默认值False而不是True,Django最近似乎改变了这个值

<a href="{% url "social:begin" "google-oauth2" %}"><button class="save btn btn-default">GET STARTED</button></a>
MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tablefor2.urls'

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_by_email',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
            'debug': DEBUG,
        },
    },
]

LOGIN_URL = '/'
LOGIN_REDIRECT_URL = '/'

WSGI_APPLICATION = 'tablefor2.wsgi.application'

SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'first_name', 'email']
SOCIAL_AUTH_USER_MODEL = 'tablefor2.Profile'

AUTHENTICATION_BACKENDS = (
    'social_core.backends.open_id.OpenIdAuth',
    'social_core.backends.google.GoogleOpenId',
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.google.GoogleOAuth',
    'django.contrib.auth.backends.ModelBackend',
)