设置 django-allauth 后,Google 提供程序未显示在 Django 管理中并出现 ImproperlyConfigured 错误

问题描述 投票:0回答:2

在我的 Django 项目中设置 Django Allauth 进行身份验证后遇到两个问题:

  1. Google 提供商选项未显示在下拉列表中 当导航到“添加社交应用程序”部分时 Django 管理面板。
  2. 当尝试访问登录页面(/enrolled/sign-in/)时,我 收到“配置错误”错误,并显示消息“未知” 提供商:谷歌”。

我按照 Django Allauth 文档中提供的设置说明进行操作,并在我的设置文件中添加了 Google 提供程序的必要配置。尽管如此,我还是面临着这些问题。

这是我的 settings.py 文件:

"""
Django settings for temp project.

Generated by 'django-admin startproject' using Django 4.2.11.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-x5242^+u0mit3om8z)e=8i$(vi!o@7$ks1om9*@7r(lo(=!3y!'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    "allauth.account.middleware.AccountMiddleware",
]

ROOT_URLCONF = 'temp.urls'

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',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]

SOCIALACCOUNT_PROVIDERS = {
    'google': {
        # For each OAuth based provider, either add a ``SocialApp``
        # (``socialaccount`` app) containing the required client
        # credentials, or list them here:
        'APP': {
            'client_id': '123',
            'secret': '456',
            'key': ''
        }
    }
}

WSGI_APPLICATION = 'temp.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

在 Django 管理中,我无法在提供商的下拉选项中看到 Google。在许多 YouTube 视频中,显示提供商名称会在您在 INSTALLED_APPS 中选择时显示。 Django 管理

现在,这是我的 django 模板:

    {% extends 'base.html' %}
    {% load static %}
    {% load socialaccount %}
    
    {% block extra_head %}
        <link rel="preload" href="{% static 'order/css/sign-in-form.css' %}" as="style">
        <link rel="stylesheet" href="{% static 'order/css/sign-in-form.css' %}">
    {% endblock %}
    
    {% block dynamic_content %}
        <div class="button-container">
            <a href="{% provider_login_url 'google' %}" class="login-button">
                <i class="fa-google fa-brands fa-xl"></i>
            </a>
        </div>
    {% endblock %}

我收到此错误:未知提供商:google 未知提供商错误

这个错误指出了这一行:

<a href="{% provider_login_url 'google' %}" class="login-button">

非常感谢您的帮助,谢谢!

django django-templates django-allauth
2个回答
1
投票

我也很认真地纠结过这个问题,终于找到了解决办法。

pip install django-allauth[socialaccount]

由于我的传统是使用 Pipenv,因此以下方法有效:

pipenv install "django-allauth[socialaccount]"


0
投票

对于 zsh,添加双引号: pip install "django-allauth[socialaccount]"

© www.soinside.com 2019 - 2024. All rights reserved.