[英]Django Channels - self.channel_layer.group_send not calling function
[英]Django channels JsonWebsocketConsumer self.send_json() error
我正在編寫一個消費者,該消費者在通過via self.send_json()發送后發送一個對話列表和一個配置文件對象(聯系人),此錯誤顯示出來,我真的不知道發生了什么,想知道您是否可以提供幫助
錯誤:
Exception inside application: 'name'
File "C:\Users\DELL\ENVS\async\lib\site-packages\channels\sessions.py",
line 183, in __call__
return await self.inner(receive, self.send)
File "C:\Users\DELL\ENVS\async\lib\site-
packages\channels\middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "C:\Users\DELL\ENVS\async\lib\site-packages\channels\consumer.py",
line 62, in __call__
await await_many_dispatch([receive], self.dispatch)
File "C:\Users\DELL\ENVS\async\lib\site-packages\channels\utils.py",
line 52, in await_many_dispatch
await dispatch(result)
File "C:\Users\DELL\ENVS\async\lib\site-packages\asgiref\sync.py", line
238, in __call__
return await asyncio.wait_for(future, timeout=None)
File
"C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib
\asyncio\tasks.py",
line 4 14, in wait_for
return await fut
File "C:\Users\DELL\AppData\Local\Programs\Python\Python37\lib
\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\DELL\ENVS\async\lib\site-packages\channels\db.py", line
14, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
File "C:\Users\DELL\ENVS\async\lib\site-packages\asgiref\sync.py", line
271, in thread_handler
return func(*args, **kwargs)
File "C:\Users\DELL\ENVS\async\lib\site-packages\channels\consumer.py",
line 105, in dispatch
handler(message)
File "C:\Users\DELL\ENVS\async\lib\site-
packages\channels\generic\websocket.py", line 60, in websocket_receive
self.receive(text_data=message["text"])
File "C:\Users\DELL\ENVS\async\lib\site-
packages\channels\generic\websocket.py", line 125, in receive
self.receive_json(self.decode_json(text_data), **kwargs)
File "E:\Personal Projects\Tolk\chat\consumers.py", line 41, in
receive_json
name = content['name']
'name'
Consumers.py:
```python
class LoadConsumer(JsonWebsocketConsumer):
def connect(self):
if self.scope['user'].is_authenticated:
self.accept()
else:
self.close()
def receive_json(self, content, **kwargs):
if content['command'] == "CHAT":
name = content['name']
data = self.load_conversation_contact(name)
else:
data = {"success": False, "errors": "no such command"}
self.send_json(data) # the error happens here
def load_conversation_contact(self, name):
conversation = self.load_conversation(name)
contact = self.load_contact()
return {
"success": True,
"conversation": conversation['conversation'],
"contact": contact['contact'],
}
def load_conversation(self, name):
conversation = Conversation.objects.filter(name=name).first()
if conversation:
serializer = ConversationSerializer(conversation)
data = serializer.data
return {"success": True, "conversation": data}
return {"success": False}
```
資料:
```python
{'success': True, 'conversation': {'type': 'couple', 'name':
'conversation_8', 'contacts': [OrderedDict([('first_name', 'limbi'),
('last_name', 'salah'), ('location', 'London, UK'), ('contact_pic',
'/media/users/1/profile/92a87524defac4511e17_avatar-male-2.jpg'),
('user',
'limbi@main.com')]), OrderedDict([('first_name', 'cool'), ('last_name',
'dude'), ('location', 'London, UK'), ('contact_pic',
'/media/users/22/profile/f210d59103d63dd2ffa2_avatar-male-4.jpg'),
('user', 'wow@gmail.com')])], 'last_message_content': 'Lorem Ipsum is
simply dummy text of the printing and typesetting industry.',
'last_message_date': 'Yesterday', 'message_set':
[OrderedDict([('content',
'Lorem Ipsum is simply dummy text of the printing and typesetting
industry.'), ('sender', 4), ('conversation', 8), ('sent', True),
('date_sent', '2019-09-01'), ('time_sent', '08:42:42.079479')])]},
'contact': {'first_name': 'limbi', 'last_name': 'salah', 'location':
'London, UK', 'contact_pic':
'/media/users/1/profile/92a87524defac4511e17_avatar-male-2.jpg',
'user':
'limbi@main.com'}}
```
我試過:1.調試以查看出什么問題2.更改名稱的值
因此JSON對象內缺少鍵
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.