关于这个错误,我使用类似的问题来设置与Django的频道消息传递。我尝试升级Django和Channels并更改requirements.txt文件。
"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语句
import chat.routing
未被识别为教程中的说明,它应该在我的IDE中。
我的目录结构也完全按照本教程
使用设置配置
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'
谢谢
答案 0 :(得分:0)
可以肯定这是问题所在。需要在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
答案 1 :(得分:0)
使用Django Channels服务器运行daphne的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应用程序不兼容。安装Daphne的1.x版本以支持Channels 1.x。
如您所见,我们可以通过daphne服务器使用HTTP
和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
如果在运行daphne服务器时看到类似HTTP/2 support not enabled (install the http2 and tls Twisted extras)
的内容,则可以运行pip install -U Twisted[tls,http2]
来更正这些错误。
答案 2 :(得分:0)
尝试以下方法,对我有用:
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
)
),
})