问题
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