In django How to schedule a daily report to be emailed

I want to know exactly how I can schedule a report to be generated and sent as an email of the visitor information log table daily at a specific time. Details of the visitor, such as name, time and time, purpose of the visit should be sent in the form of email. Using django 1.6.5 on linux.

I know about cron Django - set up a scheduled task? https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ , but it seems not all together.

I can create a template and view it in django admin gui using all the model admin settings. I can also generate csv using the actions in the admin panel. But I want the report to be generated automatically every day and sent as email without entering django. I need a complete solution for this code as I do not understand how this can be done. Please, help

+4
source share
1 answer

First create a custom management command, for example:

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' )

To create an email text and send

Then you can add a cronjob, something like

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

Run this command every night

+6
source

All Articles