Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ImproperlyConfigured - The SECRET_KEY setting must not be empty

I am running a django application using celery. I am getting a strange error in my tasks.py file, which is as follows:

from __future__ import absolute_import

from celery import shared_task
from django.contrib.auth.models import User

# some code 
# ....

Here is the error:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY must not be empty.

In my settings.py, SECRET_KEY is defined

# ...
import myproj.tasks
import djcelery

djcelery.setup_loader()

# ...
SECRET_KEY = '18730s9n9sjxamsuJSismxOIAmso102xjAs'
# ...

The application runs fine if I comment the import in the tasks.py file:

# from django.contrib.auth.models import User

and, more surprisingly, it also runs fine if I make the exact same imports in another file (e.g. testfile.py) in the same directory.

Why is this error happening?

Edit: Here is my celery.py file

from __future__ import absolute_import
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')

from django.conf import settings #noqa

app = Celery('myproj')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
like image 269
Gabriel Ilharco Avatar asked Jan 11 '16 18:01

Gabriel Ilharco


People also ask

How do I change Django settings to false?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.

What happens if you change Django secret key?

What happens if I change Django secret key? Once you change the SECRET_KEY on production, all the old sessions and cookies are invalidated, users are logged out and data in sessions are lost.

Does the Django secret key matter?

Does the Django secret key matter? Summary: The Django secret key is used to provide cryptographic signing. This key is mostly used to sign session cookies. If one were to have this key, they would be able to modify the cookies sent by the application.


1 Answers

The issue was caused by importing the celery tasks into the settings file in order to pass them to the CELERYBEAT_SCHEDULE setting.

This import caused a circular import because when the task is imported it will load the settings which only loaded up until the point where the task was imported, thus complaining that no SECRET_KEY was configured.

To solve this (and as general best practice), when configuring CELERYBEAT_SCHEDULE use the path to the task like so:

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'myapp.tasks.my_task',
        'schedule': timedelta(seconds=30),
        'args': (16, 16)
    },
}

Source http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries

like image 186
Paulo Avatar answered Nov 14 '22 22:11

Paulo