首页 > 解决方案 > Django // 版本更新后所有请求上的 Daphne 500 错误“TypeError:object HttpResponse 不能用于 'await' 表达式”

问题描述

在将 Django 从版本 3.1.13 更新到 3.2.8 后,我遇到了一个问题,我收到了 500 个响应。

以前版本的Django上没有出现此问题,只有settings.py.

如果有人可以帮助验证我的asgi.pysettings.py配置是否正确,或者确定导致问题的依赖项以便我可以跟进并提出错误,我将不胜感激。

重现问题的回购

https://github.com/oscarhermoso/bug-opentelemetry-django-asgi

出现在浏览器中的达芙妮错误

500内部服务器错误

应用程序内部异常。

ASGI 入口点

# testproject/asgi.py

import os

from django.core.asgi import get_asgi_application

# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testproject.settings')

from channels.auth import AuthMiddlewareStack  # noqa
from channels.routing import ProtocolTypeRouter, URLRouter  # noqa

import testproject.routing  # noqa

application = ProtocolTypeRouter({
    # Django's ASGI application to handle traditional HTTP requests
    "http": get_asgi_application(),

    # WebSocket chat handler
    "websocket": AuthMiddlewareStack(
        URLRouter(
            testproject.routing.websocket_urlpatterns
        )
    ),
})

已安装的应用程序和中间件

# settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'channels'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'opencensus.ext.django.middleware.OpencensusMiddleware'
]

错误转储

System check identified no issues (0 silenced).
October 09, 2021 - 10:59:03
Django version 3.2.8, using settings 'testproject.with_both'
Starting ASGI/Channels version 3.0.4 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Internal Server Error: /
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/utils/deprecation.py", line 119, in __call__
    response = self.process_response(request, response)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'coroutine' object has no attribute 'get'
Exception inside application: object HttpResponse can't be used in 'await' expression
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.8/site-packages/channels/staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "/home/vscode/.local/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/asgi.py", line 161, in __call__
    response = await self.get_response_async(request)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 150, in get_response_async
    response = await self._middleware_chain(request)
TypeError: object HttpResponse can't be used in 'await' expression
HTTP GET / 500 [0.97, 127.0.0.1:41256]

Internal Server Error: /favicon.ico
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/utils/deprecation.py", line 119, in __call__
    response = self.process_response(request, response)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'coroutine' object has no attribute 'get'
Exception inside application: object HttpResponse can't be used in 'await' expression
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.8/site-packages/channels/staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "/home/vscode/.local/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/asgi.py", line 161, in __call__
    response = await self.get_response_async(request)
  File "/home/vscode/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 150, in get_response_async
    response = await self._middleware_chain(request)
TypeError: object HttpResponse can't be used in 'await' expression
HTTP GET /favicon.ico 500 [0.90, 127.0.0.1:41256]

标签: djangoazuredjango-channelsopen-telemetryasgi

解决方案


好吧,我仍在寻找解决方案。但是,经过一些研究,我发现 get_asgi_application 存在问题。

我读过 Django 默认添加了 HTTP 协议,因此,我们不需要这一行。我删除"http": get_asgi_application(),网站后开始正常工作。

有人在这里谈论这个:https ://djangokatya.wordpress.com/2020/10/26/500-internal-server-error-daphne-http-processing-error/

无论如何,在 Channels 的 V3 文档中,他们明确要求添加"http": get_asgi_application(),......不知道没有它它是如何工作的,但它对我有用。

问题是:在此之后,我开始在 Daphne: 中遇到错误ValueError: HTTP response has not yet been started but got http.response.body。到目前为止还不能很好地理解发生了什么。


推荐阅读