38đź‘Ť
i would suggest creating your functionality as django-management-command and run it via crontab
if your command is send_newsletter
then simply
0 0 * * * python /path/to/project/manage.py send_newsletter
and you don’t need to take care of setting the settings module in this case/
11đź‘Ť
Ashok’s suggestion of running management commands via cron works well, but if you’re looking for something a little more robust I’d look into a library like Kronos:
# app/cron.py
import kronos
@kronos.register('0 * * * *')
def task():
pass
- Django – OperationalError: (2006, 'MySQL server has gone away')
- I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?
- Django crispy forms: Nesting a formset within a form
3đź‘Ť
I suggest to take a look at django-chronograph. Basically it does what you want in a simmilar way to other suggestions + it gives you a possbility to manage your cron jobs via admin panel. Cron jobs have to be implemented as django commands. You can then run all pending jobs by calling
python manage.py cron
which should be triggered by your cron.
- How to have a "random" order on a set of objects with paging in Django?
- Django Model Method or Calculation as Field in Database
- Django order_by sum of fields
1đź‘Ť
I’d recommend option 3: use the jobs system in django-extensions. The relevant extension commands are:
create_jobs
– Creates a Django jobs command directory structure for the given app name in the current directory. This is part of the impressive jobs system.runjob
– run a single maintenance job. Part of the jobs system.runjobs
– runs scheduled maintenance jobs. Specify hourly, daily, weekly, monthly. Part of the jobs system.
This lets you manage all the job handling inside of Django, so you don’t have to keep messing with the crontab.
- Django 1.7 removing Add button from inline form
- Django Celery Time Limit Exceeded?
- Django test runner fails in virtualenv on Ubuntu
- Django SimpleUploadedFile with Python 3
1đź‘Ť
I have written a few command-line applications using a method similar to your first option. I prefer to do it this way as opposed to using the DJANGO_SETTINGS_MODULE
environment variable because it feels more like a regular Python program (to me).
You should also note that you do not have to put your module in the same directory as your settings.py
; you can use the absolute Python path of your settings module:
from django.core.management import setup_environ
from project import settings
setup_environ(settings)
#The rest of your imports
PEP 8 discourages relative imports anyway.
I always install my Django applications in site-packages (/usr/lib64/python2.6/site-packages
on Gentoo) so I don’t have to worry about setting PYTHONPATH
from my crontabs, but I don’t believe that is a widely practiced method. I also like to use setuptools Automatic Script Creation so that my console scripts get put wherever they should be (/usr/bin
, for example) and are named appropriately automatically. Your first option also facilitates this.
- HTML input textbox in Django admin.py filter
- How to make TimeField timezone-aware?
- Django runserver error when specifying port
- When to use NullBooleanField in Django
- Django queryset order_by dates near today
1đź‘Ť
There is also django-cron. It is very simple to use, there is nothing else to install or set-up.
However, I am not sure about how it really works… I mean that I don’t know how the jobs are run and if they are run at all when nobody makes a request to the site. But you can try-out !
- What is the use of cleaned_data in Django
- In Django how do I notify a parent when a child is saved in a foreign key relationship?
- Django Boolean Queryset Filter Not Working
- Why are my environment variables not detected when starting up celery?
- Pidbox received method enable_events() [reply_to:None ticket:None] in Django-Celery
0đź‘Ť
Option 1 works for me. I normally have the script cd to the project directory and then do “python ./script_name.py” so that there are no mysterious path problems … lazy, but it works consistently.
- Call Django celery task by name
- Admin interface for SQLAlchemy?
- How to paginate "Change List" page in Django admin?
- Cannot read property 'module' of undefined