如何使用django通知通知用户有人对他们的帖子发表评论 [英] How to use django-notification to inform a user when somebody comments on their post

查看:155
本文介绍了如何使用django通知通知用户有人对他们的帖子发表评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在django开发了一段时间,并开发了一个整洁的网站,具有写博客,发布问题,分享内容等功能。但是还有一件事是缺少的,即为用户创建通知。 / p>

我想做的是通知用户在他们的个人资料中,每当有人对他们的帖子发表评论,或者如果他们遵循特定的文章,并且有更新,然后通知用户该更新。我已经看了许多应用程序,但我仍然很困惑如何做到这一点。



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



首先我想知道我错了,然后我真的需要一些适当的教程或指导,了解如何做。我知道如何注册通知并以适当的信号发送,但没有关于如何在模板中显示这些通知的文档,如果可以这样做。



任何

解决方案

是django通知仅适用于电子邮件通知。 p>

这是一个信号插槽,您可以添加到models.py并调整您自己的需求:



<$ p $从django.db导入模型
从django.contrib.sites.models导入站点
从django.db.models导入信息
从通知导入模型作为通知

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):
#删除这个if-block如果你想要通知批注修改
如果没有创建:
返回无

上下文= {
'注释':实例,
'site':Site.objects.get_current(),
}
recipient = []

#添加所有将同一个对象注释给收件人的用户
用于注释实例.__ class __。objects.for_model(instance.content_object):
如果comment.user不在收件人和comment.user!= instance.user:
recipients.append(comment.user)

#如果被注释的对象是用户然后通知他
如果isinstance(instance.content_object,models.get_model('auth','User')):
#如果他是他发表评论的人,那么不要将他添加到收件人
如果instance.content_object!= instance.user和instance.content_object不在收件人中:
recipient.append(instance.content_object )

notification.send(recipient,'new_comment',context)

signals.post_save.connect(new_comment,sender = models.get_model('comments','Comment'))

现在对于模板,很简单。



templates / notification / new_comment /short.txt

  {{comment.user}}对{{comment.object}}评论

templates / notification / new_comment / notice.html

 < a href ={{comment.user.get_absolute_url}}> {{comment.user}}< / a>评论了< a href ={{comment.content_object.get_absolute_url}}> {{comment.content_object}}< / a> 

templates / notification / new_comment / full.txt



$ {pre> {{comment.user}}评论了{{comment.content_object}}

评论:
{{comment.comment}}

回复:
http:// {{site.domain}} {{comment.content_object.get_absolute_url}}

警告:这是一个非常简化的,未经测试的我们的生产代码的修改。



以下是一些更多信息: p>


I have been developing in django for sometime now, and have developed a neat website having functionality such as writing blogs, posting questions, sharing content etc. However there is still one thing that is missing and i.e. creating notification for users.

What I want to do is to inform users in their profiles, whenever somebody comments on their posts, or if they are following a particular post and there is an update on it, then inform the user of that update. I have looked around many applications but I am still very confused about how to do it.

In case of using django-notification I seem to have an impression(which can be wrong) that I can use this only to inform the user via email, i.e. I cannot show these notifications in the user profile, just like we have on facebook.

Firstly I would like to know if I am wrong, and then I really need some proper tutorial or guidance on how to go about doing it. I know how to register a notification and send it on proper signal but there is no documentation on how to show these notices in a template, if this can be done.

Any guidance/tutorial/getting started doc will be deeply appreciated.

解决方案

Yes django-notifications is only designed for email notifications.

Here is a signal slot that you can add to your models.py and tweak to your own needs:

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'))

Now for templates, pretty easy.

templates/notification/new_comment/short.txt

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

templates/notification/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>

templates/notification/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 }}

Warning: it's a very simplified, untested adaptation of our production code.

Here are some more information:

这篇关于如何使用django通知通知用户有人对他们的帖子发表评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆