django ModuleNotFoundError:没有名为“channels.http”的模块

bsxbgnwa  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(326)

我发现了几个类似的问题,但没有真正帮助。我尝试更新asgiref版本,并更新了channelsdjango-eventstream。我主要是遵循django-eventstream-setup page的指令。
我的安装程序中的相关软件包:

Package            Version
------------------ ---------
asgiref            3.6.0
channels           4.0.0
Django             4.1.2
django-eventstream 4.5.1
django-grip        3.2.0
django-htmx        1.12.2
gripcontrol        4.1.0
huey               2.4.3
MarkupSafe         2.1.2
requests           2.28.1
Werkzeug           2.2.2

执行python manage.py runserver时出现的错误:

...
File "...\venv\lib\site-packages\django_eventstream\routing.py", line 2, in <module>
    from . import consumers
File "...\venv\lib\site-packages\django_eventstream\consumers.py", line 7, in <module>
    from channels.http import AsgiRequest
ModuleNotFoundError: No module named 'channels.http'

我在settings.py旁边创建了一个asgi.py文件:

import os
from django.core.asgi import get_asgi_application
from django.urls import path, re_path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import django_eventstream

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

application = ProtocolTypeRouter({
    'http': URLRouter([
        path("events/", AuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)), { 'channels': ['test'] }),
        re_path(r"", get_asgi_application()),
    ]),
})

setup.py中,我更新了asgi的使用:

WSGI_APPLICATION = "channels_test.wsgi.application"
ASGI_APPLICATION = "channels_test.asgi.application"

这是文件夹结构:

kb5ga3dv

kb5ga3dv1#

使用渠道版本3.0.5!作者django-eventstream没有更新自己的文档)

相关问题