Python social-auth-app-django 'social' 不是注册的命名空间
Python social-auth-app-django 'social' is not a registered namespace
我正在使用 Python(3.6) 和 Django(2.0) 开发一个项目,我正在尝试使用 social-auth-app-django
包集成社交登录。
在 users
应用程序中管理用户。
这是我的配置:
来自settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'phone_field',
'social_django',
]
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',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, 'templates'],
'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', # <- Here
'social_django.context_processors.login_redirect',
],
},
},
]
LOGIN_URL = 'users/login/'
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.linkedin.LinkedinOAuth2', # for Github authentication
'social_core.backends.facebook.FacebookOAuth2', # for Facebook authentication
'django.contrib.auth.backends.ModelBackend',
)
来自 users/urls.py:
app_name = 'users'
urlpatterns = [
path('signup/', views.SignUpView.as_view(), name='signup'),
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),
url(r'^auth/', include('social_django.urls', namespace='social')),
]
SOCIAL_AUTH_URL_NAMESPACE = "users:social"
来自login.html:
<div class="col-lg-12 center-aligned">
<div style="margin: auto">
<div class="or-seperator"><b>or</b></div>
<div class="social-btn text-center">
<a href="{% url 'social:begin' 'facebook-oauth2' %}" class="btn btn-primary btn-lg" title="Facebook"><i class="fa fa-facebook"></i></a>
<a href="{% url 'social_django.urls.url' %}" class="btn btn-info btn-lg" title="LinkedIn"><i class="fa fa-linkedin"></i></a>
<a href="{% url 'social:begin' 'google-oauth2' %}" class="btn btn-danger btn-lg" title="Google"><i class="fa fa-google"></i></a>
</div>
</div>
</div>
当我加载 login
模板时,出现 returns 错误:
Exception Type: NoReverseMatch
Exception Value:
'social' is not a registered namespace
根据 docs
urlpatterns = patterns('',
...
url('', include('social_django.urls', namespace='social'))
...
)
更重要的是:
SOCIAL_AUTH_URL_NAMESPACE = 'social'
当你有
SOCIAL_AUTH_URL_NAMESPACE = "users:social"
又考虑到SOCIAL_AUTH_URL_NAMESPACE一定要写成settings.py
,不能写成urls.py
url(r'^auth/', include('social_django.urls', namespace='social')),
您应该在 myproject_root/urls.py
中导入,而不是在 myapp/urls.py
中导入
我正在使用 Python(3.6) 和 Django(2.0) 开发一个项目,我正在尝试使用 social-auth-app-django
包集成社交登录。
在 users
应用程序中管理用户。
这是我的配置:
来自settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'phone_field',
'social_django',
]
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',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, 'templates'],
'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', # <- Here
'social_django.context_processors.login_redirect',
],
},
},
]
LOGIN_URL = 'users/login/'
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.linkedin.LinkedinOAuth2', # for Github authentication
'social_core.backends.facebook.FacebookOAuth2', # for Facebook authentication
'django.contrib.auth.backends.ModelBackend',
)
来自 users/urls.py:
app_name = 'users'
urlpatterns = [
path('signup/', views.SignUpView.as_view(), name='signup'),
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),
url(r'^auth/', include('social_django.urls', namespace='social')),
]
SOCIAL_AUTH_URL_NAMESPACE = "users:social"
来自login.html:
<div class="col-lg-12 center-aligned">
<div style="margin: auto">
<div class="or-seperator"><b>or</b></div>
<div class="social-btn text-center">
<a href="{% url 'social:begin' 'facebook-oauth2' %}" class="btn btn-primary btn-lg" title="Facebook"><i class="fa fa-facebook"></i></a>
<a href="{% url 'social_django.urls.url' %}" class="btn btn-info btn-lg" title="LinkedIn"><i class="fa fa-linkedin"></i></a>
<a href="{% url 'social:begin' 'google-oauth2' %}" class="btn btn-danger btn-lg" title="Google"><i class="fa fa-google"></i></a>
</div>
</div>
</div>
当我加载 login
模板时,出现 returns 错误:
Exception Type: NoReverseMatch
Exception Value:
'social' is not a registered namespace
根据 docs
urlpatterns = patterns('',
...
url('', include('social_django.urls', namespace='social'))
...
)
更重要的是:
SOCIAL_AUTH_URL_NAMESPACE = 'social'
当你有
SOCIAL_AUTH_URL_NAMESPACE = "users:social"
又考虑到SOCIAL_AUTH_URL_NAMESPACE一定要写成settings.py
,不能写成urls.py
url(r'^auth/', include('social_django.urls', namespace='social')),
您应该在 myproject_root/urls.py
中导入,而不是在 myapp/urls.py