Tag Archives: django

In web applications sometimes you want to run processes that are seperated from the Request/Response cycle, like scheduled jobs that run at a specific time of a day. Scheduling such jobs in a unix environment is very easy with cronjobs. You just need to edit the crontab file and define a time and a command(a script to be run in this case) to execute at that time.

This post is about how to schedule such Django programs, that is python programs that import and use the Django framework.

This should be trivial because a Django program is another python program. But there is an important step to be done in about for Django modules to be discovered and run. That is to specify the Django settings file.

Let’s say there is a Django project called mysite and a Django app called myapp that has a method called runJob() in a python module called jobs.py that needs to be called every 30 mins.

let’s write a python script called thescript.py that will run every 30 minutes as a cronjob that will in turn call the runjob method.

thescript.py
#######################################

import os

os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘settings’

from myapp.jobs import runJob

runJob()

###################################

For easiness save the ‘thescript.py’ in the mysite folder.

Then open a console and enter crontab -e to edit your cron file. Enter the following line.

*/30 * * * * python /path to mysite/thescript.py > logfile 2>&1

This will run the ‘thescript.py’ file every 30 minutes and any output would be directed to the logfile specified and the last ‘2>&1’ tells that no email should be sent to the administrator. For more information on how to write the crontab file have a look at this.

That is how easy it is to schedule a Django cronjob in an Unix environment.

Django-resolvetime is a Django filter to convert a python datetime object into a format that specifies the time difference between the time of the datetime and now. I developed this for a personal project of mine and decided to opensource since I could not find a filter available to get the job done.

Django is the most widely used if not the most popular web framework among those available for python. Have a look at this report by Jacob Kaplan-Moss the co-creator of Django, to get an idea about the current standing of Django among the community.

One disadvantage for Django is the lack of IDE support it has. Here it holds a clear disadvantage against it’s rival web frameworks such as Ruby on Rails. Wing IDE looks impressive but is not free. I wasn’t much impressed with Komodo and it is not free either. The best bet seams to be Netbeans 7 which promises to have good python support. Hopefully it would have satisfactory Django support as well. Currently netbeans is having a python plugin that is still in beta stage. The best alternative for me as of now is PyDev which I have been using with python for sometime now. Here I will explain how to configure PyDev to work with Django.

Here under python interpreter a python interpreter should be available. If not add one by clicking new

In System PYTHONPATH add the downloaded source folder of Django by clicking New Folder

3.) Create a new pydev project.

Go to file > new > pydev project

Enter project name, path and importantly make sure the option ‘create default src folder and add it to PYTHONPATH?’ is choosen. This is easiest way or you can manually add the source folder of the new project to PYTHONPATH by going to File > properties > PYTHONPATH

4.) Start a new Django project

Open a new console from the Operating System(console support inside pydev is avialable but is not very satisfactory)

Navigate to the newly create src folder inside the pydev project

type django-admin.py startproject ‘sitename’ (sitename should be the name of your project). This how a new project is started in Django

If your projects name is mysite the folder structure should be as follows
mysite
src
mysite

Now pydev should be configured to work with Django. All the python feature that pydev supports should be now working with Django as well.

The standard features of a modern IDE works beautifully with pydev for Django but there are some major drawbacks. Django template tags are not supported in pydev. Also the console support inside pydev is not impressive. So for now I am forced to use pydev for Django coding, the OS console for giving Django commands and nothing for Django templates.