【发布时间】:2021-11-29 00:27:36
【问题描述】:
在将 Django 从版本 3.1.13 更新到 3.2.8 后,我收到了 500 个响应。
该问题在以前版本的 Django 上没有出现,只有在 settings.py 中启用了 Django Channels 应用程序和 OpenCensus 中间件时才会出现此问题。
如果有人可以帮助验证我的 asgi.py 和 settings.py 是否配置正确,或者确定导致问题的依赖项以便我可以跟进并提出错误,我将不胜感激。
重现问题的回购
https://github.com/oscarhermoso/bug-opentelemetry-django-asgi
浏览器中出现的 Daphne 错误
500 内部服务器错误
应用程序内部异常。
Daphne
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]
【问题讨论】:
-
删除
opencensus会阻止问题发生吗? -
正确@IainShelvington。我在 Azure 中使用该库进行跟踪,需要保持安装包
-
由于您已经在 GitHub 上打开了一个问题,所以在此处发布链接以帮助其他社区成员:ASGI Django with OpencensusMiddleware erroring on all requests with TypeError: object HttpResponse can't be used in 'await' expression
-
面临同样的问题。您找到解决方法了吗?
-
@CristianFavaroCarriço - 还没有解决方法,我将继续使用 Django 3.1,直到我收到有关链接的 GitHub 问题的一些反馈。
标签: django azure django-channels open-telemetry asgi