从 django-admin 命令发送 django 信号?
我有一个不寻常的问题。在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我唯一能想到的是,信号处理程序在 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 theapp
package, everything in__init__.py
should execute, registering the listener.捷径是:
你能在这里发布你的项目结构吗?你的代码对我来说看起来不错。
The shortcut is :
Can you post your project structure here? Your code looks good to me.