Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider is not showing up in Django admin and getting ImproperlyConfigured error after setting up django-allauth #3775

Closed
mynewcodingjourney opened this issue Apr 30, 2024 · 4 comments

Comments

@mynewcodingjourney
Copy link

I'm encountering two issues after setting up Django Allauth for authentication in my Django project:

  • The Google provider option is not showing up in the dropdown list when navigating to the "Add social application" section of the Django admin panel.
  • When attempting to access the sign-in page (/enrolled/sign-in/), I'm getting an "ImproperlyConfigured" error with the message "unknown provider: google".

I'm using,
Ubuntu - Ubuntu 20.04.6 LTS (VS Code)

Here are the exact steps I followed to set up django-allauth in my project:

  1. Creating virtual environment:
python3 -m venv myvenv
  1. Activating virtual environment:
source myvenv/bin/activate
  1. Installing django and django-allauth:
pip install django
pip install django-allauth

requirements.txt:

asgiref==3.8.1
backports.zoneinfo==0.2.1
Django==4.2.11
django-allauth==0.62.1
sqlparse==0.5.0
typing-extensions==4.11.0
  1. Creating django project:
django-admin startproject googlelogin
  1. This is my settings.py:
"""
Django settings for googlelogin project.

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

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-*k8d%eq^(*g8*eeo31lnaiogpl3x60h)5y9t15jup)_txf$9u2'

# 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.github',
    '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",
]

SOCIALACCOUNT_PROVIDERS = {
    'github': {
        'SCOPE': [
            'user',
            'repo',
            'read:org',
        ],
    },
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        },
        'OAUTH_PKCE_ENABLED': True,
    }
}

ROOT_URLCONF = 'googlelogin.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',
]

WSGI_APPLICATION = 'googlelogin.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'

I've followed this page of documentation to update my settings.py:

I directly copy pasted github and google config to SOCIALACCOUNT_PROVIDERS temporarily...

  1. This is my project-level urls.py file:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
]
  1. Now, typing migrate command:
python manage.py migrate
  1. Now, I'm creating a superuser account
python manage.py createsuperuser
  1. Now at http://127.0.0.1:8000/admin/socialaccount/socialapp/add/
    1

As you can see in the provider dropdown, there are no items (such as Github or Google). However, according to this video YouTube (timestamp 7:29), there should be a mention of items there before specifying keys and after running migration command.

  1. In this condition if i'm creating a html template, configuring it and runing server again with this html:
{% extends 'base.html' %}
{% load static %}
{% load socialaccount %}

{% block extra_head %}
    <title>Sign In to X</title>
{% endblock %}

{% block dynamic_content %}
     <div class="button-container">
        <a href="{% provider_login_url 'github' %}" class="login-button">
            <i class="fa-github fa-brands fa-xl"></i>
        </a>
        <a class="login-button">
            <i class="fa-linkedin fa-brands fa-xl"></i>
        </a>
        <a href="{% provider_login_url 'google' %}" class="login-button">
            <i class="fa-google fa-brands fa-xl"></i>
        </a>
        <a class="login-button">
            <i class="fa-microsoft fa-brands fa-xl"></i>
        </a>
        <a class="login-button">
            <i class="fa-apple fa-brands fa-xl"></i>
        </a>
        <a class="login-button">
            <i class="fa-discord fa-brands fa-xl"></i>
        </a>
    </div>
{% endblock %}

For this, it sometimes gives DoesNotExist at / or ImproperlyConfigured at / errors, pointing out to <a href="{% provider_login_url 'github' %}" class="login-button">. If I remove this line, it then points out to <a href="{% provider_login_url 'google' %}" class="login-button">!

Thanks for reading!

@pennersr
Copy link
Owner

Did you install using pip install django-allauth[socialaccount] ?

@mynewcodingjourney
Copy link
Author

Did you install using pip install django-allauth[socialaccount] ?

No,

I've installed the entire package through pip install django-allauth, so it has downloaded all the social account providers, including Google and GitHub...

See this screenshot of my venv directory, which shows that django-allauth for Google exists:
6

@pennersr
Copy link
Owner

Please read here: https://docs.allauth.org/en/latest/release-notes/recent.html#backwards-incompatible-changes

The [socialaccount] extra makes sure that the dependencies (requests, jwt) for Google are installed. The google provider is always there in your env, its dependencies are not. Can you give it a try?

@mynewcodingjourney
Copy link
Author

Please read here: https://docs.allauth.org/en/latest/release-notes/recent.html#backwards-incompatible-changes

Thank you for pointing that out!

The [socialaccount] extra makes sure that the dependencies (requests, jwt) for Google are installed. The google provider is always there in your env, its dependencies are not. Can you give it a try?

I've installed the dependencies as per your suggestion, and everything seems to be working perfectly now. Appreciate the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants