我正在尝试在Django登录中使用Google创建登录,因此当我创建“使用Google登录”时,它会转到Google登录页面,但是在选择Google帐户时会显示错误,
ModuleNotFoundError at /auth/complete/google-oauth2/
No module named 'social'
我的项目urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('pages.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('auth/', include(('social_django.urls', 'social_django'), namespace='social')),
path('admin/', admin.site.urls),
]
settings.py
import os
INSTALLED_APPS = [
#...,
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'social_django',
'django',
]
MIDDLEWARE = [
#...
#...
]
ROOT_URLCONF = 'trydjango.urls'
TEMPLATES = [
{
'BACKEND': '# ...',
'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', # Google login
'social_django.context_processors.login_redirect', # Google Login
],
},
},
]
DATABASES = {
'default': {
# ...
# ...
}
}
AUTH_PASSWORD_VALIDATORS = [
# ....
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth', # for Google authentication
'social_core.backends.google.GoogleOpenId', # for Google authentication
'social_core.backends.google.GoogleOAuth2', # for Google authentication
'social_core.backends.google.GoogleOAuth',
# 'social_core.backends.github.GithubOAuth2', # for Github authentication
# 'social_core.backends.facebook.FacebookOAuth2', # for Facebook authentication
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.debug.debug',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'social.pipeline.debug.debug',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
# Google+ SignIn (google-plus)
SOCIAL_AUTH_GOOGLE_PLUS_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_PLUS_SCOPE = [
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '*************************.....'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '************************'
这里是关于错误的更多描述:
Django版本:2.1
异常类型:ModuleNotFoundError
异常值:没有名为“ social”的模块
异常 位置:/home/barmansvaps/anaconda3/lib/python3.6/site-packages/social_core/utils.py 在import_module中,第52行
Python 可执行文件:/ home / barmansvaps / anaconda3 / bin / python3
答案 0 :(得分:-1)
您是否安装了 python-social-auth 模块。请尝试此代码
pip install python-social-auth
您可以详细了解here。
更新: 如omab所建议。请避免使用未维护的过时库,而应使用social_core
当前正在维护的库