我有一个像下面这样的模型。创建实例后,我想向感兴趣的一方发送电子邮件:
class TrainStop(models.Model):
name = models.CharField(max_length=32)
notify_email = models.EmailField(null=True, blank=True)
def new_stop_created(sender, instance, created, *args, **kwargs):
# Only for new stops
if not created or instance.id is None: return
# Send the status link
if instance.notify_email:
send_mail(
subject='Stop submitted: %s' % instance.name,
message='Check status: %s' % reverse('stop_status', kwargs={'status_id':str(instance.id),}),
from_email='admin@example.com',
recipient_list=[instance.notify_email,]
)
signals.post_save.connect(new_stop_created, sender=TrainStop)
但是,该reverse
调用仅返回 URL 的路径部分。示例:/stops/9/status/
。我需要一个完整的 URL,例如http://example.com/stops/9/status/
. 我将如何检索当前网站的主机名和端口(对于不使用端口 80 的测试实例)?
我最初的想法是通过一个变量来提供settings.py
它,然后我可以根据需要访问它。但是,认为有人可能有更强有力的建议。