9

我正在用 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

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

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

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

import chat.routing

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

带设置配置

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

ASGI_APPLICATION = 'chat.routing.application'

谢谢

4

5 回答 5

9

使用daphne服务器运行我的 Django Channels 时出现这种错误routing.py

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.

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

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

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

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

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

from .wsgi import *

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

# DockerDjangoNginx is my project name
# your routing.py file should be in this location where the wsgi.py file is placed
# DockerDjangoNginx/DockerDjangoNginx/routing.py

from .wsgi import *  # add this line to top of your code
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import comapp.routing as routing

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

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

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

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

于 2019-05-30T04:02:06.843 回答
1

很确定这是问题所在。需要在 wsgi.py 旁边添加这个 asgi.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()

并启动服务器

(vEnv)$daphne <myproj>.asgi:application --port 8888
于 2018-11-07T00:00:11.127 回答
0

改变:

ASGI_APPLICATION = 'myproject.routing.application'

ASGI_APPLICATION = "myproject.asgi.application"

问题有望得到解决,请查看官方频道网站了解更多详情(https://channels.readthedocs.io/en/stable/installation.html

于 2021-10-26T07:57:34.310 回答
0

Try the following, it worked for me:

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

from chat import routing # This change

application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
    URLRouter(
        routing.websocket_urlpatterns   #This change
    )
  ),
})
于 2020-08-09T05:17:33.000 回答
0

检查你的 settings.py,你说它包括:

ASGI_APPLICATION = 'chat.routing.application'

但是聊天是你添加的应用程序,它应该是 django 项目本身的名称,所以在你说你遵循的教程中它应该是这样的:

ASGI_APPLICATION = 'mysite.routing.application'
# or
ASGI_APPLICATION = 'core.routing.application'
于 2021-09-22T15:36:52.357 回答