python - django channels message.reply_channel 没有属性发送

标签 python django django-channels

我一直在努力了解 Django 的 channel ,但我无法将消息发送到我的 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

from channels.routing import route

from RESTAPI.consumers import send_free

channel_routing = [
    route('send-free',send_free),
]

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

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

最佳答案

我假设您从这样的 View 中调用“send-free”...

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

然后 send_free 没有 message.reply_channel...

换句话说,一旦客户端将 WebSocket 数据包发送给我们,消息就会从中获取 reply_channel 属性。这将用于将消息回复给客户端...(可能到前端)

所以你真的要发送消息...?然后再次使用消费者发送...

关于python - django channels message.reply_channel 没有属性发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554631/

相关文章:

heroku - 达芙妮拒绝所有远程请求

python - 类型错误 : can only concatenate str (not "set") to str

python - 按索引复制 numpy 数组的单个轴

python - 在 Django QuerySet 中指定限制和偏移量不起作用

javascript - 受 pinterest 启发的仪表板布局

python - 与主管一起运行 Django worker 服务器?

django - 设置 Django channel

python : selecting row where y==1 and column is 0 in a matrix

python - 如何以内存有效的方式在Python中将矩阵一分为二?

django - 如何启动现有的 Django 项目?