Django,芹菜主管:密钥错误:Django.core.exceptions。配置错误:密钥设置不能为空

2024-04-28 15:12:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个django1.11.4、python3.5、gunicorn19.7.1服务器,带有supervisor、redis2.10.6和cellery 4.1.0。在

我的文件结构如下:

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

我的秘密来自:

^{pr2}$

我的主管配置文件是:

[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]

我的初始化文件:

# __init__.py

from __future__ import absolute_import
from .celery import app as celery_app

我的配置设置.py芹菜档案:

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'

错误可能来自我的设置.py文件我使用一个env变量来设置环境。(生产、开发或测试):

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

然后设置firebase凭证和googleapi凭证。然后我从公司内部获得证书,比如,萨米_firebase.py有:

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

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

   Traceback (most recent call last):
  File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/kombu/utils/objects.py", line 42, in __get__
    return obj.__dict__[self.__name__]
KeyError: 'data'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/dorian/Projects/samy/samy_python/env/bin/celery", line 11, in <module>
        sys.exit(main())
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/__main__.py", line 14, in main
        _main()
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 326, in main
        cmd.execute_from_commandline(argv)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
        super(CeleryCommand, self).execute_from_commandline(argv)))
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 281, in execute_from_commandline
        return self.handle_argv(self.prog_name, argv[1:])
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 480, in handle_argv
        return self.execute(command, argv)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 412, in execute
        ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/worker.py", line 219, in run_from_argv
        *self.parse_options(prog_name, argv, command))
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 403, in parse_options
        self.parser = self.create_parser(prog_name, command)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 419, in create_parser
        self.add_arguments(parser)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/worker.py", line 275, in add_arguments
        default=conf.worker_state_db,
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 130, in __getattr__
        return self[k]
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 431, in __getitem__
        return getitem(k)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 280, in __getitem__
        return mapping[_key]
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/collections/__init__.py", line 982, in __getitem__
        if key in self.data:
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/kombu/utils/objects.py", line 44, in __get__
        value = obj.__dict__[self.__name__] = self.__get(obj)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 148, in data
        return self.callback()
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 911, in _finalize_pending_conf
        conf = self._conf = self._load_config()
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 921, in _load_config
        self.loader.config_from_object(self._config_source)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/loaders/base.py", line 133, in config_from_object
        self._conf = force_mapping(obj)
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 50, in force_mapping
        if isinstance(m, (LazyObject, LazySettings)):
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/utils/functional.py", line 238, in inner
        self._setup()
      File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
        self._wrapped = Settings(settings_module)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/conf/__init__.py", line 129, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

另外,gunicorn工作得很好,所以如果我不启动celry程序,它就会接收到环境变量。如果我在设置.py文件,开始。在


Tags: infrompyselfenvhomelibpackages
2条回答

在env中设置密钥,如下所示:

export SECRET_KEY="#mysecret_key"

并检查:

^{pr2}$

在django设置中,更改为:

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

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

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

相关问题 更多 >