2010-03-30 48 views
1

这是我第一次使用django信号,我想挂钩评论应用程序提供的“comment_was_flagged”信号,以在评论被标记时通知我。Django“comment_was_flagged”信号

这是我的代码,但它似乎没有工作,我错过了什么?

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

def comment_flagged_notification(sender, **kwargs): 
    send_mail('testing moderation', 'testing', '[email protected]', ['[email protected]',]) 

comment_was_flagged.connect(comment_flagged_notification) 

(我只是测试的电子邮件了,但我已经向电子邮件正常发送。)

谢谢!

回答

4

我猜你已经把这段代码扔到了signals.py模块或类似的东西里。

你必须确保你的模块代码实际上是在运行时执行的。如果没有您的模型模块导入您的信号模块,您的信号侦听器将无法连接。

从Django的​​偷一个片段:

...你需要确保该 模块它在得到早期 使信号处理变得 注册的任何信号之前需要进口发送至 。这使得您的应用程序的 models.py成为 注册信号处理程序的好地方。