python - 我正在使用 django 信号来登录用户,但我遇到错误,说 "Signal receivers must be callable."我已经制作了接收器

标签 python django

views.py中,我只是创建 Message 类的对象并做了其他事情

@python_2_unicode_compatible
class Message(models.Model):
    user = models.ForeignKey(User, related_name='+',  blank=True, null=True)
    message = models.TextField(max_length=1000, blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True)
    conversation = models.ForeignKey(User, related_name='logged_in_user', blank=True, null=True)
    form_user = models.ForeignKey(User, related_name='+',  blank=True, null=True)
    is_read = models.BooleanField(default=False)


    def __str__(self):
        return self.message 

    @staticmethod
    def get_conversations(user):
        conversations = Message.objects.filter(user=user).values('conversation').annotate(
            last=Max('date')).order_by('-last')
        users = []
        for conversation in conversations:
            users.append({
                'user': User.objects.get(pk=conversation['conversation']),
                'last': conversation['last'],
                'status': 'online' if hasattr(conversation, 'logged_in_user') else 'offline',
            })
        return users


    def login_user(sender, request, user, **kwargs):
        Message(conversation=user).save()


    def logout_user(sender, request, user, **kwargs):
        try:
            u = Message.objects.get(conversation=user)
            u.delete()
        except Message.conversation.DoesNotExist:
            pass

    user_logged_in.connect(login_user)
    user_logged_out.connect(logout_user)

使用 django 信号以不同的方式登录用户非常简单,并且代码可以在我的项目中运行,但在这里我想以不同的方式实现。

最佳答案

嗯..您可以尝试用于消息目的的 channel 如果您有任何疑问,您可以从以下位置获取所有内容 [此处][1] https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django

关于python - 我正在使用 django 信号来登录用户,但我遇到错误,说 "Signal receivers must be callable."我已经制作了接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44070343/

相关文章:

mysql - Django-MySQL 启用 Row_Format=Compress with syncdb

python - Django 升级 : Static Files Not Served

django - 使用 django 和 xgettext 进行本地化

python - django manage.py runserver后127.0.0.1拒绝连接

python - 我正在使用递归来测试一个单词是否是回文。回文是向后相同的单词,例如 : race car

python - groupby.shift 时的性能问题

python - 为什么python的socket.getfqdn()有时会返回localhost.localdomain?

python - 在 Django 上的必需 selectDateWidget 上显示 empty_label

python - Django 登录表单/无效用户

python - 如果行不是以列表中的项目开头