在django如何安排将每日报告作为电子邮件发送出去

时间:2014-10-14 11:25:37

标签: python django email cron

我想知道我是如何安排生成报告并在特定时间每天作为访问者详细信息日志表的电子邮件发送的。访问者的详细信息,如姓名,进出时间,访问目的需要作为电子邮件发送。在linux上使用django 1.6.5。

我知道cron Django - Set Up A Scheduled Job? https://docs.djangoproject.com/en/dev/howto/custom-management-commands/但似乎并没有把事情放在一起。

我可以使用所有模型管理选项在django admin gui中创建模板和视图。我还可以使用管理面板中的操作生成csv。但我希望每天自动生成报告并以电子邮件形式发送,而无需登录django。我需要完整的代码解决方案,因为我不清楚如何做到这一点。请帮忙

1 个答案:

答案 0 :(得分:7)

首先创建自定义管理命令,如:

class Command(BaseCommand):

commands = ['sendreport',]
args = '[command]'
help = 'Send report'

def handle(self, *args, **options):

    '''
    Get completed sessions, send invite to vote
    '''

    reports = Log.objects.filter(date__gt=datetime.today(),date__lt=(datetime.today()+timedelta(days=2)))
    for report in reports:
        send_notification(_("Report"), _("log:%s")%report.text, 'my@email.com' )

创建电子邮件文本并发送

然后你可以添加cronjob,比如

0 0 * * * /pathtovirtualenv/python manage.py sendreport

每晚运行此命令

相关问题