django 通道配置不当:在 ASGI_APPLICATION 模块中找不到“应用程序”

L1g*_*33D 7 python django redis channels

我正在用 Django 设置频道 asgi。我曾尝试升级 Django 和 Channels。

"Cannot find %r in ASGI_APPLICATION module %s" % (name, path)
django.core.exceptions.ImproperlyConfigured: Cannot find 'app' in ASGI_APPLICATION module <MyApp>.routing
Run Code Online (Sandbox Code Playgroud)

我的路由配置按照 mysite/routing 中的教程

application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
    URLRouter(
        chat.routing.websocket_urlpatterns
    )
  ),
})
Run Code Online (Sandbox Code Playgroud)

和应该只是简单的导入语句

import chat.routing
Run Code Online (Sandbox Code Playgroud)

我的目录结构也完全按照教程

与设置配置

INSTALLED_APPS = [
'channels',
'chat',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Run Code Online (Sandbox Code Playgroud)

ASGI_APPLICATION = 'chat.routing.application'
Run Code Online (Sandbox Code Playgroud)

谢谢

Kus*_*era 9

我在使用daphne服务器运行Django Channels时遇到了这种错误。routing.py

\n
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.\n
Run Code Online (Sandbox Code Playgroud)\n

这是文档对daphne服务器的解释,

\n
\n

Daphne 是一个用于 ASGI 和 ASGI-HTTP 的 HTTP、HTTP2 和 WebSocket 协议服务器,旨在为 Django Channels 提供支持。

\n

支持协议自动协商;There\xe2\x80\x99s 不需要 URL 前缀来确定 WebSocket 端点与 HTTP 端点。

\n

注意:Daphne 2 与 Channels 1.x 应用程序不兼容,仅与 Channels 2.x 和其他 ASGI 应用程序兼容。安装 Daphne 1.x 版本以获得 Channels 1.x 支持。

\n
\n

正如你所看到的,我们可以通过daphneHTTP服务器使用和协议,而无需使用Gunicorn服务器。您可以做的只是将以下行添加到文件顶部。WSrouting.py

\n
from .wsgi import *\n
Run Code Online (Sandbox Code Playgroud)\n

所以现在你的routing.py文件应该是这样的,

\n
from .wsgi import *\n
Run Code Online (Sandbox Code Playgroud)\n

现在您可以运行daphne服务器了。

\n
(venv) [root@t2mdocker]#daphne -b 0.0.0.0 -p 8000 DockerDjangoNginx.routing:application\n2019-05-30 03:33:06,390 INFO     Starting server at tcp:port=8000:interface=0.0.0.0\n2019-05-30 03:33:06,391 INFO     HTTP/2 support enabled\n2019-05-30 03:33:06,391 INFO     Configuring endpoint tcp:port=8000:interface=0.0.0.0\n2019-05-30 03:33:06,392 INFO     Listening on TCP address 0.0.0.0:8000\n
Run Code Online (Sandbox Code Playgroud)\n

HTTP/2 support not enabled (install the http2 and tls Twisted extras)如果您在运行daphne服务器时看到类似的内容,您可以运行pip install -U Twisted[tls,http2]来纠正这些错误。

\n


L1g*_*33D 1

很确定这就是问题所在。需要将此 asgi.py 文件添加到 wsgi.py 旁边

import os
import django

from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<myproj>.settings")
django.setup()
application = get_default_application()
Run Code Online (Sandbox Code Playgroud)

并启动服务器

(vEnv)$daphne <myproj>.asgi:application --port 8888
Run Code Online (Sandbox Code Playgroud)