Django Scheduled Tasks: Setup Guide

In Django, you can configure scheduled tasks using the third-party library django-crontab. Here are the configuration steps:

  1. the Python module django-crontab
pip install django-crontab
  1. The configuration file settings.py
  2. The Django crontab module
  3. List of installed applications
INSTALLED_APPS = [
    ...
    'django_crontab',
]
  1. 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.

  1. 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
  1. 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.

bannerAds