Django,Celery主管:密钥错误:django.core.exceptions.ImproperlyConfigured:SECRET_KEY设置不能为空

时间:2017-12-11 04:08:37

标签: python django redis celery

我有一个django 1.11.4,python3.5,gunicorn 19.7.1服务器,带有主管,redis 2.10.6和芹菜4.1.0。

我的文件结构如下:

([-+]?(?:\d+\.\d*|\.?\d+)(?:[eE][-+]?\d+)?)

我得到了我的秘密:

samy_python
├── celery.py
├── __init__.py
├── settings
├── urls.py
├── wsgi.py
website
├── static
├── templates
├── samy
|    ├── samy_firebase.py
|    ├── tasks.py
├── views.py

芹菜的主管conf文件是:

# settings.py
SECRET_KEY = os.environ.get("secret_KEY")

我的芹菜文件:

[program:gunicorn]

....
environment = secret_KEY="12345"
.....

[program:celery]
directory=/home/username/Projects/samy/samy_python
command=/home/username/Projects/samy/samy_python/env/bin/celery --app=samy_python.celery.app worker -B -l info
user=username
stdout_logfile=/var/log/celery/celery.log
stderr_logfile=/var/log/celery/celery.log
autostart=false
autorestart=false
startsecs=10

我的初始文件:

# celery.py

from __future__ import absolute_import
import os
import logging
logger = logging.getLogger('myapp.celery.py')

from celery import Celery
from django.conf import settings

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "samy_python.settings")

# Celery App
# TODO
# [START Celery App]
app = Celery('samy_python')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# [END Celery App]

我的settings.py文件中的芹菜配置:

# __init__.py

from __future__ import absolute_import
from .celery import app as celery_app

错误可能来自我的settings.py文件中我使用env变量来设置环境。 (制作,开发或测试):

BROKER_URL = 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

然后我设置了firebase凭据和Google Api凭据。然后,我从内部获取凭据,例如samy_firebase.py,其中包含:

DEBUG = False
DJANGO_TESTING = False
DJANGO_DEVELOPMENT = False
DJANGO_ENV = os.environ.get('DJANGO_ENV')

if DJANGO_ENV == 'DJANGO_TESTING':
    DJANGO_TESTING = True
elif DJANGO_ENV == 'DJANGO_DEVELOPMENT':
    DJANGO_DEVELOPMENT = True

我不确定最后一部分是否相关,但这是我一直收到的错误:

from django.conf import settings
if settings.DJANGO_TESTING == True:
    logger.info('Firebase Config: Test DB')

此外,gunicorn工作正常,所以如果我不启动芹菜程序它接收环境变量。如果我将密钥设置为" 12345"在settings.py文件中,它启动。

2 个答案:

答案 0 :(得分:0)

将env中的secret_KEY设置为:

export SECRET_KEY="#mysecret_key"

并检查:

echo $SECRET_KEY

在django设置中,请更改为:

SECRET_KEY = os.environ.get("SECRET_KEY")

详细参考检查:Read and Set Environment Variables

答案 1 :(得分:0)

将环境变量添加到主管conf文件的[program:celery]部分。目前它只设置为gunicorn