Django Scheduled Tasks: Setup Guide
In Django, you can configure scheduled tasks using the third-party library django-crontab. Here are the configuration steps:
- the Python module django-crontab
pip install django-crontab
- The configuration file settings.py
- The Django crontab module
- List of installed applications
INSTALLED_APPS = [
...
'django_crontab',
]
- configuration file for Python settings
CRONJOBS = [
('*/5 * * * *', 'yourappname.tasks.my_task')
]
The ‘*/5 * * * *’ means the task will be executed every 5 minutes and ‘yourappname.tasks.my_task’ is the function that will be executed.
- Run the following command in the root directory of your Django project to add a scheduled task to the system’s Cron:
python manage.py crontab add
- Starting a Django project will trigger scheduled tasks to be executed according to the configuration.
Please note that the django-crontab library relies on the system’s Cron service, so make sure that Cron service is installed on the system.