9

いつかdjangoで開発していて、ブログを書く、質問を投稿する、コンテンツを共有するなどの機能を持つきちんとしたWebサイトを開発しました。まだ欠けているものが1つあり、ユーザーのための通知を作成しています。django-notificationを使って投稿者の誰かが自分の投稿にコメントしたときに通知する方法

私がしたいのは、プロファイルのユーザーに自分の投稿にコメントするとき、または特定の投稿をフォローしてその上に更新がある場合、そのアップデートをユーザーに知らせることです。私は多くのアプリケーションの周りを見てきましたが、私はまだそれを行う方法について非常に混乱しています。

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

まず、私が間違っているかどうかを知りたいと思っています。次に、実際にやる方法について適切なチュートリアルやガイダンスが必要です。通知を登録して適切な信号で送信する方法はわかっていますが、これを行うことができればテンプレートにこれらの通知を表示する方法に関するドキュメントはありません。

ガイダンス/チュートリアル/初心者の方に深く感謝します。

答えて

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

テンプレート/通知/ 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/

{{ comment.user }} commented on {{ comment.content_object }} 

Comment: 
{{ comment.comment }} 

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

full.txt

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

注:ジャンゴ - 1.7非推奨post_syncdb信号ここ

は、いくつかのより多くの情報がある:私はその可能な信号を生成する方法である

+0

その部分ははっきりしていますが、その見解はどうですか?どのように私はユーザープロファイルを表示するビューでこの通知を受け取ることができます。これらすべての通知をユーザプロファイルに表示したい – Sachin

+0

この 'new_comment'は、これがinbuilt django-commentsフレームワークで動作するか、自分のカスタムフレームワークを持つ必要があります。投稿フレームワーク – Sachin

+1

へのリンクがどこで行われているのですか?通知モデルのソースに表示されているように、django-notificationsはすばらしいユーザプロファイルのアクティビティリストを作成しません:https://github.com/jtauber/django-notification/blob/master/notification/models。py通知のメッセージは、1人の人のために事前レンダリングする必要があります....また、new_commentはdjango.contrib.commentsで動作することを意図しています。 django-notifications:メール通知に便利です。それ以上のものはありません。 – jpic

関連する問題