python - Getting link of latest article in Models
问题描述
I have a post signal in my Models so that when I create a new post thru the admin, it will send a mail. It works, but I want the message to include the link of the article I just created. How can I do this?
def send_mails(sender, **kwargs):
if kwargs['created']:
message = 'Hello!\nWe have a new post: mysite.net/{latest article id}\nEnjoy!\n\nKind regards,\nThe Analyst'
subject = 'New Article Published!'
to = Email.objects.values_list('email', flat=True).distinct()
from_email = settings.EMAIL_HOST_USER
send_mail(subject, message, from_email, to, fail_silently=True)
post_save.connect(send_mails, sender=Post)
解决方案
The instance that was saved is passed as instance
parameter:
def send_mails(sender, instance, created, **kwargs):
if created:
message = f'Hello!\nWe have a new post: mysite.net/{instance.id}\nEnjoy!\n\nKind regards,\nThe Analyst'
subject = 'New Article Published!'
to = Email.objects.values_list('email', flat=True).distinct()
from_email = settings.EMAIL_HOST_USER
send_mail(subject, message, from_email, to, fail_silently=True)
推荐阅读
- python - 如何告诉 Pillow 网格是什么以及如何在其中放置文本以使其不会超过网格块?
- javascript - TypeError:client.on 不是函数(discord.js)
- python - 在python中自动下载所有必要的库
- spring-boot - MockRestServiceServer 看到先前测试的请求
- excel - 在函数和子之间传递变量
- c++ - C ++:使用else而不是if else时出现分段错误
- javascript - 如何使用函数运行输入值
- flutter - 如何将 DataCell 添加到 DataRow?
- vmware - 打印屏幕键在 VMware ESXi 中不起作用
- eclipse - 如何在纸莎草软件中增加类图的文本大小?