[Django]-Django Celery scheduled task django.core.exceptions.ImproperlyConfigured

1πŸ‘

βœ…

The error message clearly states:

django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This means that you either have to define a environment variable called DJANGO_SETTINGS_MODULE or call the method settings.configure in your code before you access the settings.

I would suggest to set the environment variable DJANGO_SETTINGS_MODULE.

Documentation: https://docs.djangoproject.com/en/1.11/topics/settings/#designating-the-settings

πŸ‘€Jens

3πŸ‘

The best practice when using Django with Celery is to have your Celery app configured by Django’s settings file.

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

This uses app.config_from_object() and also sets up your project to automatically discover tasks from tasks.py files in your Django installed apps.

πŸ‘€Casey Kinsey

Leave a comment