Django time zone problem

This is a Django bug specific to Windows and it took me half a day to figure out because I trusted the datetime module! Basically if you set your TIME_ZONE in the settings.py file like so:

TIME_ZONE = 'Australia/Sydney'

The datetime and time modules will always return the UTC date.

The strange thing is that if you use “python manage.py shell” you get the bug but if you just use “python” you don’t get the bug.

Correct time:

c:\project>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2006, 9, 28, 12, 11, 38, 109000)

Django time:

c:\project>python manage.py shell --plain
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2006, 9, 28, 3, 12, 16, 468000)

Possible solutions:

Creating a Django cron job

I couldn’t find a way to directly call a Python function in a Django application view from the command line. It doesn’t seem like it is a common thing to do from my Google search attempts. In this example I have a function defined to download a few web sites once a day using a custom made Django model. The project is called mytestproject and the application is called mytestapp. Here is the views.py file:

import urllib2
from models import WebSite
def daily_job():
    for site in WebSite.objects.all():
        page_html = urllib2.urlopen(site.url).read()
        do_something_with_page_html(page_html)

To run this function from the command line an optimist would create a python script that looks like this:

#!/usr/bin/env python
from mytestapp.views import daily_job
daily_job()

Running this will give you an exception about your DJANGO_SETTINGS_MODULE environment variable not being defined:

EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Lets change the script a little to conform with Django’s demands.

#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from mytestapp.views import daily_job
daily_job()

Please note that according to the documentation, DJANGO_SETTINGS_MODULE should be ‘mytestproject.settings’ instead of just ‘settings’. Using the project name in DJANGO_SETTINGS_MODULE will cause troubles in our situation because Django does some tricky things to your path before importing the settings module. For our needs it isn’t necessary to do this.

Of course you can make this script a little more generic so you can run an arbitrary script from your cron job if you feel the need to:

import sys
import os

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

module_name = sys.argv[1]
function_name = ' '.join(sys.argv[2:])

exec('import %s' % module_name)
exec('%s.%s' % (module_name, function_name))

I’ll call this script run.py. To run my daily_job function:

python run.py mytestapp.views daily_job()

And your modnight cron job entry should look something like:

0 0 * * * python /path/to/mytestproject/run.py mytestapp.views daily_job()

And there you have it. I hope this will save you some time!