Standalone scripts and cron jobs are integral part of any significantly complex web based application. We use them in our applications for health monitoring, mails sending, computing scores and ratings, deferred tasks and job queues such as video processing etc.
To make a python script work which uses django's functionality, you need
to have the required environment variables set: your application's path
in your PYTHON path and name of the settings module in
DJANGO_SETTINGS_MODULE. If you are using Unix, you can set these in
bash_profile so that each time you log in, the environment
variables are already set.
For instance, if your project's name is myproject, then add these lines
(with appropriate changes) in
export PYTHONPATH="/home/path/to/myproj/" export DJANGO_SETTINGS_MODULE="myproj.settings"
Now you can write and execute a python script which imports models and views of myproj.
CronJobs using the django models or views also need these environment variables which can be set at the top of the crontab file. Open the crontab file using this command on shell:
$crontab -e # Edit the crontab file
Put these lines at the top to pass required environment variables to any subsequent cronjobs
If you use webmin for scheduling cronjobs, you can create environment variables from from the menu.