gpt4 book ai didi

python - 将 Django channel 与现有 Django 3.1 wsgi 代码集成时出错

转载 作者:行者123 更新时间:2023-12-05 06:09:11 25 4
gpt4 key购买 nike

在 Django 中添加 channel 时出现以下错误。

应用程序内部异常:init() 在 Django 3.1 中为 channel 添加 ASGI_APPLICATION 时需要 1 个位置参数

Exception inside application: __init__() takes 1 positional argument but 2 were given
Traceback (most recent call last):
File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/channels/routing.py", line 160, in __call__
send,
File "/home/dell/Desktop/s/s/s-env/lib/python3.6/site-packages/asgiref/compatibility.py", line 33, in new_application
instance = application(scope)
TypeError: __init__() takes 1 positional argument but 2 were give

应用程序在 WSGI 配置上工作正常。只有在添加 ASGI_APPLICATION = 'app.routing.application' 时才会出现上述错误。

由于它显示源自静态文件的错误,我尝试再次生成静态文件,但这没有帮助。

我正在使用默认的 django asgi 文件:

"""
ASGI config for mevsyou project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 's.settings.local')

application = get_asgi_application()

s/routing.py:

from channels.routing import ProtocolTypeRouter, URLRouter
from users import routing

application = ProtocolTypeRouter({
'http': URLRouter(routing.urlpatterns),
})

最佳答案

您的代码适用于 channel 2,而您使用的是 channel 3。

在 channel 3 中,您不应该有 routing.py。但是您的 asgi.py 文件应该如下所示:

import os

from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = ProtocolTypeRouter({
"http": get_asgi_application(),
})

事实上,ASGI_APPLICATION应该设置为'app.asgi.application'

See Channels docs for 3.x

关于python - 将 Django channel 与现有 Django 3.1 wsgi 代码集成时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64901758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com