pennersr / django-allauth

Integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication.
https://allauth.org
MIT License
9.43k stars 3.02k forks source link

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

Closed mynewcodingjourney closed 4 months ago

mynewcodingjourney commented 4 months ago

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

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
  2. Activating virtual environment:
    source myvenv/bin/activate
  3. 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
  4. Creating django project:
    django-admin startproject googlelogin
  5. 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:
- [https://docs.allauth.org/en/latest/installation/quickstart.html](https://docs.allauth.org/en/latest/installation/quickstart.html)
- [For Github](https://docs.allauth.org/en/latest/socialaccount/providers/github.html)
- [For Google](https://docs.allauth.org/en/latest/socialaccount/providers/google.html)

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

6. 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')), ]

7. Now, typing migrate command:

python manage.py migrate

8. Now, I'm creating a superuser account

python manage.py createsuperuser

9. Now at `http://127.0.0.1:8000/admin/socialaccount/socialapp/add/`
![1](https://github.com/pennersr/django-allauth/assets/168514137/26e35494-724e-4edd-a41a-0c365eaf7065)

As you can see in the provider dropdown, there are no items (such as Github or Google). However, according to this video [YouTube](https://youtu.be/RyB_wdEZhOw?si=2nDejWyihKjzjvjn) **(timestamp 7:29)**, there should be a mention of items there before specifying keys and after running migration command.

11. 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 %}

Sign In to X

{% endblock %}

{% block dynamic_content %}

{% 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 commented 4 months ago

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

mynewcodingjourney commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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!