Django3.x - Heroku - raise ImproperlyConfigured("The SECRET_KEY setting must not be empty."

Posted

技术标签:

【中文标题】Django3.x - Heroku - raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")【英文标题】: 【发布时间】:2020-05-15 05:35:30 【问题描述】:

我需要你的帮助同事。我正在尝试在 Heroku 上部署我的 Django 项目,但我遇到了一个看似简单的错误,但我已经努力调试了好几天。我会感谢详细的帮助,我是新手。提前谢谢你。

背景 我有隐藏文件夹名称,因为它也是我要注册的域名。 我在 windows10 机器和 Heroku PaaS 上有 python 3.6.8 和 Django 2.1.1。起初我使用 Django 2.2,然后降级到 Django 2.1。通过命令pipenv install djang0==2.1.1 项目在本地/开发服务器上完美运行,即python manage runserver 工作。 密钥设置为SECRET_KEY = os.environ.get('SECRET'),我也尝试过硬编码SECRET_KEY = 'my-secret-key',但没有区别。 我在 wsgi.py 和 manage.py 中正确设置了os.environ.setdefault('DJANGO_SETTINGS_MODULE', '*foldername.settings.testing')

其他设置 我的 Heroku Procfile 有 web: gunicorn *foldername.wsgi:application --log-file -。我有一个requirements 和一个runtime.txt 文件。

错误当我运行$ heroku run python manage.py migrate

Running python manage.py migrate on ⬢ thawing-thicket-02648... up, run.2948 (Free)
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 224, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 36, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 14, in <module>
    from django.db.migrations.autodetector import MigrationAutodetector
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 11, in <module>
    from django.db.migrations.questioner import MigrationQuestioner
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/questioner.py", line 9, in <module>
    from .loader import MigrationLoader
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/loader.py", line 8, in <module>
    from django.db.migrations.recorder import MigrationRecorder
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 9, in <module>
    class MigrationRecorder:
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 22, in MigrationRecorder
    class Migration(models.Model):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 126, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

运行$ heroku logs --tail 产生

2020-01-29T15:56:40.652606+00:00 app[web.1]: self.reap_workers()
2020-01-29T15:56:40.652609+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2020-01-29T15:56:40.652893+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2020-01-29T15:56:40.652896+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2020-01-29T15:56:40.745655+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-29T15:56:40.725433+00:00 heroku[web.1]: Process exited with status 1
2020-01-29T16:21:03.239311+00:00 app[api]: Starting process with command `python manage.py migrate` by user developers@email.com
2020-01-29T16:21:11.782005+00:00 heroku[run.2948]: State changed from starting to up
2020-01-29T16:21:11.639515+00:00 heroku[run.2948]: Awaiting client
2020-01-29T16:21:11.664869+00:00 heroku[run.2948]: Starting process with command `python manage.py migrate`
2020-01-29T16:21:16.264816+00:00 heroku[run.2948]: State changed from up to complete
2020-01-29T16:21:16.241764+00:00 heroku[run.2948]: Process exited with status 1
2020-01-29T16:23:33.250194+00:00 heroku[web.1]: State changed from crashed to starting
2020-01-29T16:23:39.991409+00:00 heroku[web.1]: Starting process with command `gunicorn foldername.wsgi --log-file -`
2020-01-29T16:23:42.075462+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-01-29T16:23:42.076104+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [4] [INFO] Listening at: http://0.0.0.0:34607 (4)
2020-01-29T16:23:42.076210+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [4] [INFO] Using worker: sync
2020-01-29T16:23:42.080161+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [10] [INFO] Booting worker with pid: 10
2020-01-29T16:23:42.131810+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [11] [INFO] Booting worker with pid: 11
2020-01-29T16:23:42.302136+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [10] [ERROR] Exception in worker process
2020-01-29T16:23:42.302140+00:00 app[web.1]: Traceback (most recent call last):
2020-01-29T16:23:42.302160+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-01-29T16:23:42.302163+00:00 app[web.1]: worker.init_process()
2020-01-29T16:23:42.302165+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-01-29T16:23:42.302167+00:00 app[web.1]: self.load_wsgi()
2020-01-29T16:23:42.302169+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-01-29T16:23:42.302171+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-01-29T16:23:42.302173+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-01-29T16:23:42.302176+00:00 app[web.1]: self.callable = self.load()
2020-01-29T16:23:42.302178+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-01-29T16:23:42.302180+00:00 app[web.1]: return self.load_wsgiapp()
2020-01-29T16:23:42.302182+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-01-29T16:23:42.302184+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-01-29T16:23:42.302186+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-01-29T16:23:42.302188+00:00 app[web.1]: mod = importlib.import_module(module)
2020-01-29T16:23:42.302190+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-01-29T16:23:42.302193+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-01-29T16:23:42.302195+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T16:23:42.302197+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T16:23:42.302199+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-01-29T16:23:42.302201+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-01-29T16:23:42.302203+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-01-29T16:23:42.302205+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-01-29T16:23:42.302207+00:00 app[web.1]: File "/app/foldername/wsgi.py", line 15, in <module>
2020-01-29T16:23:42.302209+00:00 app[web.1]: application = get_wsgi_application()
2020-01-29T16:23:42.302211+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-01-29T16:23:42.302213+00:00 app[web.1]: django.setup(set_prefix=False)
2020-01-29T16:23:42.302215+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
2020-01-29T16:23:42.302217+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2020-01-29T16:23:42.302219+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
2020-01-29T16:23:42.302221+00:00 app[web.1]: self._setup(name)
2020-01-29T16:23:42.302224+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
2020-01-29T16:23:42.302226+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2020-01-29T16:23:42.302228+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 126, in __init__
2020-01-29T16:23:42.302230+00:00 app[web.1]: raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
2020-01-29T16:23:42.302240+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2020-01-29T16:23:42.302620+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-01-29T16:23:42.322533+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [11] [ERROR] Exception in worker process
2020-01-29T16:23:42.322537+00:00 app[web.1]: Traceback (most recent call last):
2020-01-29T16:23:42.322539+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-01-29T16:23:42.322542+00:00 app[web.1]: worker.init_process()
2020-01-29T16:23:42.322544+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-01-29T16:23:42.322545+00:00 app[web.1]: self.load_wsgi()
2020-01-29T16:23:42.322547+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-01-29T16:23:42.322549+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-01-29T16:23:42.322552+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-01-29T16:23:42.322554+00:00 app[web.1]: self.callable = self.load()
2020-01-29T16:23:42.322556+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-01-29T16:23:42.322558+00:00 app[web.1]: return self.load_wsgiapp()
2020-01-29T16:23:42.322560+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-01-29T16:23:42.322562+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-01-29T16:23:42.322564+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-01-29T16:23:42.322566+00:00 app[web.1]: mod = importlib.import_module(module)
2020-01-29T16:23:42.322568+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-01-29T16:23:42.322570+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-01-29T16:23:42.322572+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T16:23:42.322574+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T16:23:42.322576+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-01-29T16:23:42.322578+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-01-29T16:23:42.322580+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-01-29T16:23:42.322582+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-01-29T16:23:42.322584+00:00 app[web.1]: File "/app/foldername/wsgi.py", line 15, in <module>
2020-01-29T16:23:42.322586+00:00 app[web.1]: application = get_wsgi_application()
2020-01-29T16:23:42.322588+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-01-29T16:23:42.322590+00:00 app[web.1]: django.setup(set_prefix=False)
2020-01-29T16:23:42.322592+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
2020-01-29T16:23:42.322594+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2020-01-29T16:23:42.322596+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
2020-01-29T16:23:42.322598+00:00 app[web.1]: self._setup(name)
2020-01-29T16:23:42.322600+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
2020-01-29T16:23:42.322602+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2020-01-29T16:23:42.322604+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 126, in __init__
2020-01-29T16:23:42.322606+00:00 app[web.1]: raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
2020-01-29T16:23:42.322609+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2020-01-29T16:23:42.322958+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-01-29T16:23:42.449777+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [4] [INFO] Shutting down: Master
2020-01-29T16:23:42.469241+00:00 app[web.1]: [2020-01-29 16:23:42 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-01-29T16:23:42.572344+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-29T16:23:42.557638+00:00 heroku[web.1]: Process exited with status 3

没有heroku run 的正常$ python manage.py migrate 部分会执行迁移,但应用程序仍然无法启动,并且从$ heroku logs --tail 回溯的错误仍然存​​在且相同

See my folder structure & wsgi.py

See my manage.py

Settings just to show secret key is in

【问题讨论】:

【参考方案1】:

已经过去了很长时间,但如果我看到这个,我会节省几个小时。我遇到了同样的问题,因为我忘记在 Heroku 配置变量中设置 SECRET_KEY 变量。

【讨论】:

以上是关于Django3.x - Heroku - raise ImproperlyConfigured("The SECRET_KEY setting must not be empty."的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 d3 部署 Rails 6 应用程序。在本地运行但 Heroku 构建中断:ModuleNotFoundError: Module not found: Error: Can't resol

CoffeeScript 未在 Rails 3.1 中更新

《浮筏 Floating Raft》入选 RAI 英国皇家人类学协会电影节.

Android SDK] 加载 SDK 时出错:Error: Error parsing C:\Users\Atul Rai\android-sdks\system-images\android-22

石墨烯 django 端点是不是同时需要 X-Csrftoken 和 CsrfCookie?

Django简介 安装下载 app概念 主要目录介绍