У меня есть проект Django
, который имеет несколько приложений и использует Django Channels 2.1.2
для своих возможностей WebSockets (для уведомлений и сообщений чата).
Когда я бегу daphne -b 127.0.0.1 -p 8000 myproject.asgi:application
Я получаю ошибку
'ERROR Exception inside application: Cannot import BACKEND 'channels_redis.core.RedisChannelLayer' specified for default`
Трассировка ниже
127.0.0.1:59202 - - [25/Apr/2019:10:23:09] "WSDISCONNECT /messages/bob" - -
127.0.0.1:59215 - - [25/Apr/2019:10:23:11] "WSCONNECTING /messages/bob" - -
2019-04-25 10:23:11,952 ERROR Exception inside application: Cannot import BACKEND 'channels_redis.core.RedisChannelLayer' specified for default
File "/Users/me/myapp/lib/python3.7/site-packages/channels/sessions.py", line 175, in __call__
return await self.inner(receive, self.send)
File "/Users/me/myapp/lib/python3.7/site-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/Users/me/myapp/lib/python3.7/site-packages/channels/consumer.py", line 42, in __call__
self.channel_layer = get_channel_layer()
File "/Users/me/myapp/lib/python3.7/site-packages/channels/layers.py", line 344, in get_channel_layer
return channel_layers[alias]
File "/Users/me/myapp/lib/python3.7/site-packages/channels/layers.py", line 76, in __getitem__
self.backends[key] = self.make_backend(key)
File "/Users/me/myapp/lib/python3.7/site-packages/channels/layers.py", line 46, in make_backend
return self._make_backend(name, config)
File "/Users/me/myapp/lib/python3.7/site-packages/channels/layers.py", line 69, in _make_backend
"Cannot import BACKEND %r specified for %s" % (self.configs[name]["BACKEND"], name)
Cannot import BACKEND 'channels_redis.core.RedisChannelLayer' specified for default
У меня активирована виртуальная среда, и все установлено правильно.
settings.py (базовый файл)
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [os.environ.get('REDIS_URL', 'localhost'),6379],
},
},
}
routing.py
(находится на том же уровне, что и settings.py
)
from django.conf.urls import url
from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator
from chat.consumers import ChatConsumer
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
# below is our websocket URL
url(r"^messages/(?P<username>[\w.@+-]+)", ChatConsumer),
]
)
)
)
})
base.py
Я с радостью включу consumers.py
и base.py
, если это уместно.