Best way to schedule email reports to users in Django

為{幸葍}努か 提交于 2019-12-08 10:54:24

问题


So this question is focused more around best practice and advice for Django.

Essentially, I want to schedule email reports on Django to be triggered on two events:

  • Weekly report email with some stats, news etc
  • Report triggered on event in a system (i.e. a save on a model)

Should this be done directly in Django through scheduled tasks? Or are there any other tools one could use?


回答1:


Regarding a weekly scheduled task, the most straightforward approach might be to create a new custom management command, and have a cron or Windows Task Scheduler call that command. This actually already has an answer here, along with other possible options for you to consider:

Django - Set Up A Scheduled Job?

Note: If you're using a virtualenv, make sure to have the cron call the management command via the python binary in the virtualenv, not the one in the system path.

As for a triggered action based on an application event or condition, two thoughts:

  • You could set up a listener for a post-save signal on your model. When the signal is received, the email could be sent via the receiver. You can read up on signals here.
  • Django's send_mail email wrapper is straightforward enough that you could also use that directly in your view.


来源:https://stackoverflow.com/questions/45443358/best-way-to-schedule-email-reports-to-users-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!