【问题标题】:Can't send signals to the user无法向用户发送信号
【发布时间】:2020-02-26 20:14:19
【问题描述】:

enter image description here我正在通过 django 通知向用户发送通知.. 我有用户名正则表达式在 html 上工作,所以任何人都用@username 发布它会发布并且 html 是可链接的,所以点击@username 它会进入用户名个人资料页面。现在我正在使用 django 信号来匹配用户名并打印出用户名。但是当我使用通知发送通知时。我找不到收件人(将收到通知的用户)。它给了我一个错误ValueError at /post/new/ Cannot assign "<_sre.SRE_Match object; span=(0, 4), match='@boy'>": "Notification.recipient" must be a "User" instance.

我的模型.py:

class post(models.Model):
parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_pics', null=True, blank=True)
video = models.FileField(upload_to='post_videos', null=True, blank=True)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)

objects = postManager()

def __str__(self):
    return self.title

class Meta:
    ordering = ['-date_posted', 'title']

def get_absolute_url(self):
        return reverse ('blog-home')

def total_likes(self):
    return self.likes.count()


def post_save_receiver(sender, instance, created, *args,**kwargs):
    if created and not instance.parent:
        user_regex = r'@(?P<username>[\w.@+-]+)'
        m = re.search(user_regex, instance.content)
    if m:
        username = m.group("username")

        notify.send(instance.author, recipient=m, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

post_save.connect(post_save_receiver, sender=post)

【问题讨论】:

    标签: django-models django-signals


    【解决方案1】:

    recipient 不能Match 对象:

    notify.send(instance.author, <s>recipient=m</s>, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

    您可以尝试通过以下方式获取User 对象:

    if m:
        try:
            recipient = User.objects.get(username=m.group('username'))
        except (User.DoesNotExist, User.MultipleObjectsReturned):
            pass
        else:
            notify.send(instance.author, recipient=recipient, actor=instance.author, verb='tagged you', nf_type='tagged_by_one_user')

    【讨论】:

    • 还有一个问题:我的项目目录中有通知网址。但我收到一些关于 url 的错误。我的意思是 mysites url(用于通知,因为我正在使用 django 通知):`re_path('^inbox/notifications/', include(notifications.urls, namespace='notifications')), ` 和我链接的 base.html navbnar 中的网址:` Profile notifications `错误显示`Reverse for 'notifications' not found。 `@WillemVanOnsem
    • 因为它有一个命名空间,它应该是{% url 'notifications:notifications' %}。但冒号后面的部分取决于通知中的具体视图。
    • 在答案中,如果我输入多个用户名,它会给我一个用户名。如果它们被标记,则发送相同的信号。我正在考虑用 findall 替换搜索。它会起作用吗@WillemVanOnsem
    • @Cavinblake:通常是的。当然,您需要对匹配项进行迭代
    • 嗨。还有一件事,正如我告诉你的那样,信号只对一个用户有效。如果我使用两个或三个用户名,它只会将信号发送给第一个用户名,而其他两个用户根本没有收到任何信号。具体来说,当我只输入一个用户名时,它会向该用户名发送信号,但如果我输入两个或三个用户名(用户名用户名用户名),它只会向第一个用户名发送信号,而不会发送其他用户名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多