ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 当有人评论他们的帖子时,如何使用django-notification通知用户

2019-09-30 13:58:27  阅读:254  来源: 互联网

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


我已经在django开发了一段时间了,并开发了一个整洁的网站,其功能包括撰写博客,发布问题,共享内容等.但是仍然有一件事情遗失,即为用户创建通知.

我想要做的是在用户的个人资料中告知用户,无论何时有人评论他们的帖子,或者他们是否关注某个帖子并且有更新,然后通知用户该更新.我查看了很多应用程序,但我仍然对如何做到这一点感到很困惑.

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

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

任何指导/教程/入门文档将深表感谢.

解决方法:

是的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信号

以下是一些更多信息:

> documentation
> a blog post by stringfellow

标签:django-signals,django-apps,python,django,django-notification
来源: https://codeday.me/bug/20190930/1835859.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有