Using Django in a Cron Job

63 views
Skip to first unread message

Joe

unread,
Aug 9, 2006, 11:56:31 AM8/9/06
to Django users
I would like to set up a python script that runs every night to send
out an email subscription. I would like to access a Django model that
has the user's email addresses, as well as use the Django sendmail
function. Can someone help me understand what imports/path setup I
have to do to get this working? (Note: I will not be setting up the
Cron job, nor do I know now. I will simply be giving a python script
to our IT staff for them to set up).

Thanks for the help,

Joe

Waylan Limberg

unread,
Aug 9, 2006, 12:05:03 PM8/9/06
to django...@googlegroups.com
Access your data the same way as you would in the shell. From there it
wouldn't be much different than view code that sends out an email.

--
----
Waylan Limberg
way...@gmail.com

Joe

unread,
Aug 9, 2006, 12:20:07 PM8/9/06
to Django users
Thanks for the tip! However, when I use the shell in Django, I use the
command : python manage.py shell. I was just wondering if there was
some behind the scenes setup (in the path, for instance) that was being
done that I would have to do on the Cron job.

Jeremy Dunck

unread,
Aug 9, 2006, 12:41:51 PM8/9/06
to django...@googlegroups.com
On 8/9/06, Joe <josep...@gmail.com> wrote:
>

DJANGO_SETTINGS_MODULE=python.module.path.to.project.settings.file
python yourjob.py

Joe

unread,
Aug 9, 2006, 12:59:27 PM8/9/06
to Django users
Thanks a bunch!

Tyson Tate

unread,
Aug 9, 2006, 1:06:34 PM8/9/06
to django...@googlegroups.com

Here's what I use:

------
import sys, os

sys.path.append('/home/ohgoditb/django_projects')
sys.path.append('/home/ohgoditb/django_src/django/bin')
sys.path.append('/home/ohgoditb/django_src')
sys.path.append('/home/ohgoditb/python_libs/lib/python2.3/site-
packages')
sys.path.append('/home/ohgoditb/python_libs/bin/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'fallingbullets.settings'

from fallingbullets.apps.links import models
models.sync_magnolia_links()

from fallingbullets.apps.photos import models
models.sync_flickr_photos()
-------

Then just call "python that_file.py" from the cron job.

I think some of my paths in my $PATH are redundant and you'll notice
my fun little namespace collision with 'models', but it works well
enough for me. 'fallingbullets' is the name of the project.

Regards,
Tyson

Steven Armstrong

unread,
Aug 9, 2006, 1:08:52 PM8/9/06
to django...@googlegroups.com

%<------------------------------
#!/usr/bin/env python

import sys
sys.path.append('/path/to/django')
sys.path.append('/path/to/your/projects')

import os

# assuming /path/to/your/projects/myproject/settings.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

from myproject.myapp.models import GreenStripedPotatoes

...

%<------------------------------

Alternatively you can add django and your projects folder to the
PYTHONPATH, and define DJANGO_SETTINGS_MODULE as an environment variable
before starting your script

e.g.:
$ export PYTHONPATH="$PYTHONPATH:/path/to/django:/path/to/your/projects"
$ export DJANGO_SETTINGS_MODULE="myproject.settings"
$ python /path/to/my/cronscript.py


The second method is more flexible as your sysadmin can write a shell
script that sets up the environment and then runs your script. Like this
your script can be used on different servers without changing it.

hope this helps
cheers
Steven

JHeasly

unread,
Aug 9, 2006, 3:52:50 PM8/9/06
to Django users
Steven Armstrong wrote:
> On 08/09/06 17:56, Joe wrote:
> > I would like to set up a python script that runs every night to send
> > out an email subscription. I would like to access a Django model that
> > has the user's email addresses, as well as use the Django sendmail
> > function. Can someone help me understand what imports/path setup I
> > have to do to get this working? (Note: I will not be setting up the
> > Cron job, nor do I know now. I will simply be giving a python script
> > to our IT staff for them to set up).

Also, if you're using Django's send_mass_mail, note that "As in
send_mail(), recipients in the same recipient_list will all see the
other addresses in the e-mail messages's 'To:' field." (from
http://www.djangoproject.com/documentation/email/#send-mass-mail)

I think if you want to use a "Bcc:" header, straight Python may be the
way to go. As the e-mail doc says, have a look at
http://www.python.org/doc/current/lib/module-smtplib.html (and
http://www.python.org/doc/current/lib/module-email.html).

Regards,
John

Reply all
Reply to author
Forward
0 new messages