11

私はしばらくの間djangoで開発しており、ブログの作成、質問の投稿、コンテンツの共有などの機能を備えたすっきりとしたWebサイトを開発しました。しかし、不足していることが1つあります。つまり、ユーザーへの通知を作成することです。

私がやりたいのは、誰かが投稿にコメントしたとき、または特定の投稿をフォローしていて更新がある場合は、プロフィールでユーザーに通知し、その更新をユーザーに通知することです。私は多くのアプリケーションを見回しましたが、それを行う方法についてはまだ非常に混乱しています。

使用する場合、django-notificationこれは電子メールでユーザーに通知するためにのみ使用できるという印象があります(間違っている可能性があります)。つまり、Facebookのように、これらの通知をユーザープロファイルに表示することはできません。

まず、自分が間違っているかどうかを知りたいのですが、それから、それを実行する方法についての適切なチュートリアルまたはガイダンスが本当に必要です。通知を登録して適切なシグナルで送信する方法は知っていますが、これらの通知をテンプレートに表示する方法についてのドキュメントはありません。

ガイダンス/チュートリアル/入門ドキュメントをいただければ幸いです。

4

1 に答える 1

12

はい、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 }}

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 }}

警告: これは非常に単純化された、テストされていない製品コードの適応です。

注 : Django-1.7 は post_syncdb シグナルを非推奨にしました

さらに詳しい情報を次に示します。

于 2011-12-22T12:15:06.463 に答える