Django频道消息回复频道无属性发送

2024-10-03 06:18:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在努力了解Django的频道,但我无法将我的信息发送到我的websocket。在

这是我的consumers.py

import logging
from django.contrib.sites.models import Site
from django.utils import timezone
from channels import Group
from .models import *
import json

def send_free(message):
    try:
        pi = PInformation.objects.get(
            pk=message.content.get('pk'),
        )
    except Parkplatzinformationen.DoesNotExist:
        logging.error("PI not found!")
        return

    try:
        message.reply_channel.send({
        "text": 1,
    })
    except:
        logging.exception('Problem sending %s' % (pi.name))

我的routing.py

^{pr2}$

我得到错误AttributeError: 'NoneType' object has no attribute 'send'。但是它确实得到了PInformation对象,所以它确实工作了一点。我在保存对象后马上调用它。在

你能给我一些提示吗?The Getting Started guide像我一样使用它。在


Tags: 对象djangofrompyimportsendmessageget
1条回答
网友
1楼 · 发布于 2024-10-03 06:18:32

我假设您是这样从您的角度调用"send-free"。。。在

Channel('send-free').send({'message': 'your message'})

那么send_free没有{}。。。在

换句话说,一旦WebSocket packet is sent to us by a client,那么消息就会从中获取reply_channel属性。它将用于向客户端回复消息。。。(可能对前台说)

所以你真的想发个信。。。?然后再次使用消费者发送。。。在

相关问题 更多 >