【问题标题】:How to use django-notification to inform a user when somebody comments on their post当有人评论他们的帖子时如何使用 django-notification 通知用户
【发布时间】:2012-01-26 01:41:40
【问题描述】:

我已经在 django 中开发了一段时间,并开发了一个简洁的网站,该网站具有撰写博客、发布问题、共享内容等功能。但是仍然缺少一件事,即为用户创建通知。

我想做的是在他们的个人资料中通知用户,每当有人在他们的帖子上出现,或者如果他们正在关注特定帖子并且有更新,然后通知用户该更新。我环顾了许多应用程序,但我仍然对如何做到这一点感到非常困惑。

如果使用django-notification,我似乎有一种印象(可能是错误的),我只能使用它通过电子邮件通知用户,即我不能在用户个人资料中显示这些通知,就像我们有在脸书上。

首先我想知道我是否错了,然后我真的需要一些适当的教程或指导来指导如何去做。我知道如何注册通知并在适当的信号下发送,但没有文档说明如何在模板中显示这些通知(如果可以的话)。

任何指导/教程/入门文档将不胜感激。

【问题讨论】:

    标签: python django django-signals django-apps django-notification


    【解决方案1】:

    是的 django-notifications 仅用于电子邮件通知。

    这是一个信号槽,您可以将其添加到您的 models.py 并根据自己的需要进行调整:

    from django.db import models
    from django.contrib.sites.models import Site
    from django.db.models import signals
    from notification import models as notification
    
    def create_notice_types(app, created_models, verbosity, **kwargs):
        notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted")
    signals.post_syncdb.connect(create_notice_types, sender=notification)
    
    def new_comment(sender, instance, created, **kwargs):
        # remove this if-block if you want notifications for comment edit too
        if not created:
            return None
    
        context = {
            'comment': instance,
            'site': Site.objects.get_current(),
        }
        recipients = []
    
        # add all users who commented the same object to recipients
        for comment in instance.__class__.objects.for_model(instance.content_object):
            if comment.user not in recipients and comment.user != instance.user:
                recipients.append(comment.user)
    
        # if the commented object is a user then notify him as well
        if isinstance(instance.content_object, models.get_model('auth', 'User')):
            # if he his the one who posts the comment then don't add him to recipients
            if instance.content_object != instance.user and instance.content_object not in recipients:
                recipients.append(instance.content_object)
    
        notification.send(recipients, 'new_comment', context)
    
    signals.post_save.connect(new_comment, sender=models.get_model('comments', 'Comment'))
    

    现在是模板,非常简单。

    模板/通知/new_comment/short.txt

    {{ comment.user }} commented on {{ comment.object }}
    

    模板/通知/new_comment/notice.html

    <a href="{{ comment.user.get_absolute_url }}">{{ comment.user }}</a> commented <a href="{{ comment.content_object.get_absolute_url }}">{{ comment.content_object }}</a>
    

    模板/通知/new_comment/full.txt

    {{ comment.user }} commented on {{ comment.content_object }}
    
    Comment:
    {{ comment.comment }}
    
    Reply on: 
    http://{{ site.domain }}{{ comment.content_object.get_absolute_url }}
    

    警告:这是对我们生产代码的非常简化、未经测试的改编。

    注意:Django-1.7 已弃用 post_syncdb 信号

    这里有更多信息:

    【讨论】:

    • 这就是我如何生成可以发送的信号的方式,这部分很清楚,但是视图呢。如何在显示用户配置文件的视图中接收此通知。我想在用户个人资料中显示所有这些通知
    • 这个new_comment 将适用于内置的 django-cmets 框架,或者我需要有我的自定义框架。我的 cmets 框架的链接在哪里
    • django-notifications 不会成为一个很好的用户配置文件活动列表,正如您在 Notification 模型的源代码中看到的那样:github.com/jtauber/django-notification/blob/master/notification/… 通知的消息必须为一个预渲染person .... 另外:new_comment 旨在与 django.contrib.cmets 一起使用。 django-notifications:恕我直言,非常适合电子邮件通知。不多也不少。
    • 那么我很认真地等待你更新文档,以便我可以使用 django-subscription,因为我确实需要向用户配置文件发送更新的功能
    • 你不知道怎么联系我?文档中有我的 IRC 联系人,您在 github 上打开了一个问题,我对此进行了回复,stackoverflow 上也有一个聊天对话,并且在您的消息上方有一个聊天讨论的链接。也就是说,我不会为你做这个项目。因此,如果您想制作 facebook 克隆,您最好开始阅读代码。我刚刚从新的结帐中测试了用户详细信息视图,它可以工作。我的猜测是,您需要付出更多的努力,而不是一直放弃并一直问我。你也应该尝试自己做事。
    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2011-11-05
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多