Django“comment_was_flaged”信号

发布于 2024-08-26 18:44:16 字数 607 浏览 5 评论 0原文

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

这是我的代码,但它似乎不起作用,我错过了什么吗?

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', 'test@localhost', ['[email protected]',])

comment_was_flagged.connect(comment_flagged_notification)

(我现在只是测试电子邮件,但我保证电子邮件可以正确发送。)

谢谢!

This is my first time working with django signals and I would like to hook the "comment_was_flagged" signal provided by the comments app to notify me when a comment is flagged.

This is my code, but it doesn't seem to work, am I missing something?

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', 'test@localhost', ['[email protected]',])

comment_was_flagged.connect(comment_flagged_notification)

(I am just testing the email for now, but I have assured the email is sending properly.)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-09-02 18:44:16

我猜您已将此代码放入 signals.py 模块或类似的模块中。

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

从 Django 的信号文档中窃取片段:

...您需要确保
它所在的模块很早就被导入了
这样信号处理就得到了
在任何信号需要之前注册
被发送。这使得您的应用程序
models.py 是一个放置的好地方
信号处理程序的注册。

I'm guessing you've thrown this code in a signals.py module, or something similar.

You have to make sure your module code is actually getting executed at runtime. If none of your model modules import your signals module, your signal listeners won't be getting connected.

Stealing a snippet from Django's signals documentation:

... you'll need to make sure that the
module it's in gets imported early on
so that the signal handling gets
registered before any signals need to
be sent. This makes your app's
models.py a good place to put
registration of signal handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文