当有人发表评论时,如何使用django-notification通知用户

当有人发表评论时,如何使用django-notification通知用户,第1张

当有人发表评论时,如何使用django-notification通知用户

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

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

from django.db import modelsfrom django.contrib.sites.models import Sitefrom django.db.models import signalsfrom notification import models as notificationdef 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信号

以下是更多信息:

  • 文件资料
  • stringfellow的博客文章


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5601794.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存