如何解决这个 django-private-chat 错误?
Posted
技术标签:
【中文标题】如何解决这个 django-private-chat 错误?【英文标题】:How do I solve this django-private-chat error? 【发布时间】:2020-04-25 10:58:21 【问题描述】:所以我一直在尝试将 django-private-chat 实施到现有问题中,但总是遇到错误。因此,我尝试运行在 Github 上的 django-private-chat 下找到的示例项目,我收到了同样的错误。感谢您的帮助!
25.04.20 10:45:49:ERROR:Error in connection handler
Traceback (most recent call last):
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/websockets/server.py", line 191, in handler
await self.ws_handler(self, path)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django_private_chat/handlers.py", line 259, in main_handler
user_owner = get_user_from_session(session_id)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django_private_chat/utils.py", line 15, in get_user_from_session
session = Session.objects.get(session_key=session_key)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/query.py", line 411, in get
num = len(clone)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/query.py", line 258, in __len__
self._fetch_all()
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
cursor = self.connection.cursor()
File "/opt/anaconda3/envs/testEnv/lib/python3.7/site-packages/django/utils/asyncio.py", line 24, in inner
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
25.04.20 10:45:49:DEBUG:server ! failing OPEN WebSocket connection with code 1011
25.04.20 10:45:49:DEBUG:server - state = CLOSING
25.04.20 10:45:49:DEBUG:server > Frame(fin=True, opcode=8, data=b'\x03\xf3', rsv1=False, rsv2=False, rsv3=False)
25.04.20 10:45:49:DEBUG:server x half-closing TCP connection
25.04.20 10:45:49:DEBUG:server - event = connection_lost(None)
25.04.20 10:45:49:DEBUG:server - state = CLOSED
25.04.20 10:45:49:DEBUG:server x code = 1006, reason = [no reason]
【问题讨论】:
显示您的代码。 因为我自己的项目和在 github 上找到的示例都遇到了这个问题,所以这里是示例链接,因为它是不太复杂的项目:link to github 这看起来像django_private_chat
中的一个错误,可能他们不完全支持 django3+(这就是引发此错误的原因)。
已经8天了,你解决了这个问题吗?
【参考方案1】:
在 Django 3 之后不支持 Django 私人聊天。尝试在您的 settings.py 文件中输入此内容
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
【讨论】:
【参考方案2】:Django Private Chat2 最高支持 django 4
【讨论】:
以上是关于如何解决这个 django-private-chat 错误?的主要内容,如果未能解决你的问题,请参考以下文章