首页 文章

Django社交登录:'AsgiRequest'对象没有属性'session'

提问于
浏览
1

尝试使用social-app-django(2.1.0)时出错,抛出的错误是:

'AsgiRequest'对象没有属性'session'

  • 运行通道1.1.8,Django 2.0.3,asgi-redis 1.2.0,python 3.6

  • 通过python manage.py runserver(开发环境)运行而不是uwgsi或nginx .

尝试访问/ social / login / google-oauth2 /(使用Google帐户进行身份验证)时会发生这种情况 .

我已经读过将 MIDDLEWARE_CLASSES 更改为 MIDDLEWARE 但我们已经使用Django> = 1.9一段时间,所以它已经被更改了 .

回溯如下:

Traceback(最近一次调用最后一次):文件“/lib/python3.6/site-packages/django/core/handlers/exception.py”,第35行,内部响应= get_response(请求)文件“/ lib / python3 .6 / site-packages / django / core / handlers / base.py“,第128行,在_get_response response = self.process_exception_by_middleware(e,request)File”/lib/python3.6/site-packages/channels/handler . py“,第243行,在process_exception_by_middleware中返回super(AsgiHandler,self).process_exception_by_middleware(exception,request)文件”/lib/python3.6/site-packages/django/core/handlers/base.py“,第126行,in _get_response response = wrapped_callback(request,* callback_args,** callback_kwargs)文件“/lib/python3.6/site-packages/django/views/decorators/cache.py”,第44行,在_wrapped_view_func response = view_func(request,* args,** kwargs)文件“/lib/python3.6/site-packages/social_django/utils.py”,第38行,在包装器中请求.social_strategy = load_strategy(请求)文件“/lib/python3.6/site- packages / social_django / utils.py“,l ine 23,在load_strategy中返回get_strategy(STRATEGY,STORAGE,request)文件“/lib/python3.6/site-packages/social_core/utils.py”,第279行,在get_strategy中返回策略(存储,* args,** kwargs )文件“/lib/python3.6/site-packages/social_django/strategy.py”,第39行,在init self.session = request.session if request else {} AttributeError:'AsgiRequest'对象没有属性'session'

中间件如下(添加因为其他 WsgiRequest has no attribute session 与中间件有关):

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.auth.middleware.SessionAuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.middleware.security.SecurityMiddleware",

    "social_django.middleware.SocialAuthExceptionMiddleware",

    "django.middleware.cache.UpdateCacheMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.cache.FetchFromCacheMiddleware",
]

1 回答

  • 0

    问题出在中间件上 . 我从Django> = 1.9升级到2.0.3 . SessionAuthenticationMiddleware已被删除 . 调试中间件时我发现了这个:

    ImportError:模块“django.contrib.auth.middleware”没有定义“SessionAuthenticationMiddleware”属性/类

    解决方案是删除它 . 之所以没有在它未知之前将其记录下来 .

    你可以在这里阅读更多相关信息:

    SessionAuthenticationMiddleware removed release notes

相关问题