【问题标题】:Django notification on comment submissionDjango 评论提交通知
【发布时间】:2010-09-27 13:30:59
【问题描述】:

我正在使用 Django 的 contrib.cmets,想了解以下内容。

是否有任何实用程序或应用程序可以插入到应用程序中,以便在对项目发表评论时向您发送通知?

我并没有真正使用过信号,所以请稍微描述一下。

这是我想出来的。

from django.contrib.comments.signals import comment_was_posted
from django.core.mail import send_mail

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

def comment_notification(request):
    user = request.user
    message = "123"
    notification.send([user], "new comment", {'message': message,}) 

    comment_was_posted.connect(comment_notification)

【问题讨论】:

  • 取消.connect() 呼叫。事实上,它只有在 comment_notification() 被调用时才会运行。
  • 您能否详细描述一下您是如何将其连接起来的?粘贴的代码sn-p修改了哪些文件?

标签: django django-models django-signals django-comments


【解决方案1】:

酌情将django.contrib.comments.signals.comment_was_posted 连接到notification.models.send()

【讨论】:

    【解决方案2】:

    您必须使用comment_was_posted 信号注册您的comment_notification 函数。

    from django.contrib.comments.signals import comment_was_posted
    
    if "notification" in settings.INSTALLED_APPS:
        from notification import models as notification
    
        def comment_notification(sender, comment, request):
            user = request.user
            message = "123"
            notification.send([user], "new comment", {'message': message,}) 
    
        comment_was_posted.connect(comment_notification)
    

    【讨论】:

    • 如果我使用内置的注释标签来呈现我的表单和 cmets 列表,这段代码实际上会去哪里?
    • 你必须把它放在某个地方,所以代码被启动,前放在models.py的底部。这样当 django 在 runserver 期间验证模型时就会启动它
    【解决方案3】:

    我不知道有什么应用程序(很肯定会有一些东西在那里),但推出自己的应用程序相当简单。您可以点击Comment 模型的comment_was_posted 信号来调用将向您发送电子邮件的函数。

    【讨论】:

    • 我在原帖中添加了一些代码,也许你可以快速看看它为什么不起作用,我之前没有真正处理过通知或信号网络。
    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 2019-01-09
    • 2012-10-23
    • 1970-01-01
    • 2011-04-18
    • 2021-08-07
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多