简体   繁体   English

social-auth-app-django'social'不是注册的名称空间

[英]social-auth-app-django 'social' is not a registered namespace

Python(3.6.7) and Django(2.1), trying to integrate social-auth-app-django . Python(3.6.7)和Django(2.1),试图集成social-auth-app-django

Unlike this post , I've declared SOCIAL_AUTH_URL_NAMESPACE but it doesn't work. 这篇文章不同,我已经声明了SOCIAL_AUTH_URL_NAMESPACE但是它不起作用。

Configuration : 配置:

settings.py : settings.py:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "Microlly",
    "social_django",
]

AUTHENTICATION_BACKENDS = [
    'social_core.backends.github.GithubOAuth2',
    'social_core.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]

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"
]

ROOT_URLCONF = "microblogging.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",
            "social_django.context_processors.backends",
            "social_django.context_processors.login_redirect",
        ]
    },
}
]

WSGI_APPLICATION = "microblogging.wsgi.application"

SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY="SECRET"
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET="SECRET"

SOCIAL_AUTH__KEY="ID"
SOCIAL_AUTH__SECRET="SECRET"

# Password validation
# https://docs.djangoproject.com/en/2.1/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"},
]

STATIC_ROOT = os.path.join(BASE_DIR, "static")

LOGIN_REDIRECT_URL = "/accounts/"

EMAIL_BACKEND = (
"django.core.mail.backends.console.EmailBackend"
)  # During development only

urls.py : urls.py:

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.urls import include, path, reverse_lazy

from Microlly import views

app_name = "Microlly"


urlpatterns = [
path("", views.index, name="index"),
path("accounts/", include("django.contrib.auth.urls")),
url('^api/v1/', include("social_django.urls", namespace='social'))
]

login.html login.html

<a href="{% url "social:begin" "google-oauth2" %}">Google+</a>

And the returned error is : 返回的错误是:

NoReverseMatch at /accounts/login/ NoReverseMatch位于/ accounts / login /
'social' is not a registered namespace “社交”不是注册的名称空间

So, how can I register this namespace , or is the issue anywhere else ? 那么, 我该如何注册该名称空间 ,还是其他地方出现问题?

contrary to what you might think the order in which you import your django apps matters a lot in django, it might be a problem in which your app is using the contrib.auth yet it is calling some parts of it before it is instantiated. 相反,你可能会认为在你输入你的Django的应用程序的问题了很多 Django的顺序 ,它可能是在您的应用程序使用contrib.auth但它是调用它的某些部分被实例化之前的一个问题。

INSTALLED_APPS = [
"Microlly",   <-------------move down
"social_django",   <--------------move down
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

try putthing those custom ones on the bottom and tell me if it persists. 尝试将那些自定义的放在底部,并告诉我它是否仍然存在。

as you can see, on the link you posted the custom apps are called the last ones so they can use auth and admin 如您所见,在您发布的链接上,自定义应用称为最后一个,因此它们可以使用auth和admin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM