从 django-admin 命令发送 django 信号?

发布于 2024-10-20 15:43:31 字数 1011 浏览 1 评论 0原文

我有一个不寻常的问题。在我的 Django 应用程序中,我使用信号发送电子邮件。 除了从 django-admin 命令发出的信号 - django.core.management.base.NoArgsCommand(通过 manage.py 运行)之外,所有信号均有效。

我检查了不同地方的信号,除了这个地方以外都有效。

这是我发送信号的代码:

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    help = "Send email advertisement expiration reminder to users"

    def handle_noargs(self, **options):
        from app.models import Advertisement, User
        from app.signals import ad_expires
        from datetime import datetime
        start=datetime(datetime.now().year, datetime.now().month, datetime.now().day+4,0,0)
        end=datetime(datetime.now().year,datetime.now().month,datetime.now().day+4,23,59)
        ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lte=end)
        for ad in ads:
            ad_expires.send(self,ad=ad, user=ad.user)
        print "Expiration reminders sent to %s users" % len(ads)

我做错了什么吗?

另外,有没有更简单的方法来检查一天内的日期?

I have an unsual problem. In my Django application I use signals to send emails.
All of signals work except for the one fired from django-admin command - django.core.management.base.NoArgsCommand (which is run through manage.py).

I checked my signal in different places, it works except for this place.

Here's the code where I send the signal:

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    help = "Send email advertisement expiration reminder to users"

    def handle_noargs(self, **options):
        from app.models import Advertisement, User
        from app.signals import ad_expires
        from datetime import datetime
        start=datetime(datetime.now().year, datetime.now().month, datetime.now().day+4,0,0)
        end=datetime(datetime.now().year,datetime.now().month,datetime.now().day+4,23,59)
        ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lte=end)
        for ad in ads:
            ad_expires.send(self,ad=ad, user=ad.user)
        print "Expiration reminders sent to %s users" % len(ads)

Am I doing something wrong?

Also, is there any easier way to check date within one day?

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

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

发布评论

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

评论(2

终陌 2024-10-27 15:43:32

我唯一能想到的是,信号处理程序在 django-admin 函数执行时尚未注册。您可以通过在侦听器之前添加 print 语句并运行管理命令来检查这一点。

尝试将信号监听器放入 app/__init__.py 文件中。由于您正在访问 app 包,因此 __init__.py 中的所有内容都应该执行,并注册侦听器。

The only thing I can think of, is that the signal handler hasn't been registered at the time the django-admin function executes. You can check this by preceding the listener with a print statement and running your management command.

Try putting the signal listener into the app/__init__.py file. Since you're accessing the app package, everything in __init__.py should execute, registering the listener.

柠北森屋 2024-10-27 15:43:31

捷径是:

start = datetime.now() + timedelta(days=4)
end = start + timedelta(days=1)
ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lt=end)

你能在这里发布你的项目结构吗?你的代码对我来说看起来不错。

The shortcut is :

start = datetime.now() + timedelta(days=4)
end = start + timedelta(days=1)
ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lt=end)

Can you post your project structure here? Your code looks good to me.

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