AttributeError: у объекта 'set' нет атрибута 'decode'
Я начал изучать Django для школьного проекта и делаю проект чата с использованием websocket-сервера, но при перезагрузке приложения я получаю эту ошибку, хотя я нигде не вызываю decode(). Я использую Django Channels с Daphne.
вот мой consumers.py
from channels.generic.websocket import WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def disconnect(self, code):
pass
вот мой routing.py
from django.urls import path
from . import consumers
websocket_urlpatterns = [
# path('chat/', consumers.ChatConsumer.as_asgi())
path('chat/', consumers.ChatConsumer.as_asgi())
]
вот мой asgi.py
import chat.routing
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django_channels_jwt_auth_middleware.auth import JWTAuthMiddlewareStack
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AllowedHostsOriginValidator(
JWTAuthMiddlewareStack(
URLRouter(chat.routing.websocket_urlpatterns)
)
)
})
Я получил этот откат
WebSocket HANDSHAKING /chat/ [127.0.0.1:49878]
WebSocket CONNECT /chat/ [127.0.0.1:49878]
Exception inside application: 'set' object has no attribute 'decode'
Traceback (most recent call last):
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/routing.py", line 62, in __call__
return await application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/security/websocket.py", line 37, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/django_channels_jwt_auth_middleware/auth.py", line 36, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/middleware.py", line 24, in __call__
return await self.inner(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/routing.py", line 116, in __call__
return await application(
^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/consumer.py", line 94, in app
return await consumer(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/consumer.py", line 58, in __call__
await await_many_dispatch(
File "/Users/macbook/programacao/pap/env/lib/python3.12/site-packages/channels/utils.py", line 57, in await_many_dispatch
await task
AttributeError: 'set' object has no attribute 'decode'
WebSocket DISCONNECT /chat/ [127.0.0.1:49878]
Внутри файла settings.py добавьте этот фрагмент кода:
CHANNEL_LAYERS={
'default': {
'BACKEND':'channels_redis.core.RedisChannelLayer',
'CONFIG':{
'hosts': [('localhost', 6379)],
}
},
}
Кроме того, убедитесь, что ваш Redis работает правильно.