【发布时间】:2026-01-27 13:15:02
【问题描述】:
在拉了几个小时的头发之后,我希望 * 可以帮助我解决这个问题。
我创建了两个信号来运行自动电子邮件功能。这些信号是通过修改我的 admin.py 文件中的 save_model 来触发的。
唯一的问题是......当通过 Django 开发服务器运行时,两个信号都有效(发送自动电子邮件),但在远程服务器上测试时只有一个信号有效。我不明白这是怎么可能的,我一直没能找到错误。
我知道这里没有太多信息可以使用。对这里可能发生的事情有什么建议吗?
对于文件/目录结构,这些信号保存在位于此特定应用程序目录中的 signals.py 文件中。该信号是通过修改同一目录中我的 admin.py 文件中的 save_model 定义来触发的。
以下是信号定义:
notify_status_change_signal = Signal(providing_args=['status', 'restaurant', 'delivery_date', 'contact_email', 'contact_phone'])
notify_on_change_signal = Signal(providing_args=['organization', 'identifier', 'id', 'restaurant', 'delivery_date'])
@receiver(notify_on_change_signal)
def notify_on_change(sender, organization, identifier, id, restaurant, delivery_date, signal, *args, **kwargs):
"This is the signal that only functions when run through the local development server"
order_id = identifier + str(id)
subject = r'A change has been made to order %s' % order_id
body = """
System Message: The details for order %s (%s) have been updated by the client.""" % (order_id, organization)
send_mail(subject, body, 'System Notification <system@expressdelivery.com>', [admin_email], fail_silently=False)
@receiver(notify_status_change_signal)
def notify_status_change(sender, status, restaurant, delivery_date, contact_email, contact_phone, signal, *args, **kwargs):
"""This is the signal that works on both servers"""
stat_body = {
'Confirmed':"""
Message 1 """,
'Placed': """
Message 2 """,
'En Route': """
Message 3 """,
'Completed':"""
Message 4 """,
}
#Auto email
from_addr = 'Order Status <status@expressdelivery.com>'
to_addrs = [contact_email]
subject = u'Order %s %s status changed to %s.' % (restaurant, delivery_date, status)
body = ''
for stat_label, description in stat_body.iteritems():
if status == stat_label: body = description
if status == "Confirmed" or status == "En Route" or status == "Completed":
send_mail(subject, body, from_addr, to_addrs, fail_silently=False)
#Auto text message
if status == 'En Route':
client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
message = client.sms.messages.create(to=contact_phone,
from_="5555555555",
body="Your order, %s %s, is now en route." % (restaurant, delivery_date))
【问题讨论】:
-
用一点代码,也许吧?
-
你能发布你的信号定义和你的文件/目录结构吗?可能是测试服务器设置python路径的方式不同
-
你在哪里连接信号?
-
对不起,我忘了复制那个代码。信号通过 @receiver 装饰器连接到 signals.py 中。
标签: python django django-models django-admin django-signals