Django Heroku Collectstatic 仅在部署时出错

Django Heroku Collectstatic error only on deploy

我浏览了很多其他 Whosebug 和论坛页面,它们的问题非常相似,但是 none 他们的解决方案有效。

该错误是在部署到 heroku 时在 python manage.py collectstatic --noinput 测试命令期间引起的。 运行 python manage.py collectstatic --noinput 在我的本地项目上运行没有错误。

这是我的 requirements.txt:

dj-database-url==0.5.0
Django==2.0
django-heroku==0.3.1
gunicorn==20.0.4
psycopg2==2.8.6
python-dotenv==0.15.0
pytz==2021.1
whitenoise==5.2.0

我的settings.py:

import os
import django_heroku
from dotenv import load_dotenv

load_dotenv()

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = os.getenv("SECRET_KEY")

...

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

django_heroku.settings(locals())

这是我的文件树:

.
├── Procfile
├── README.md
├── db.sqlite3
├── manage.py
├── .env
├── project_polus
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── settings.py
│   ├── static
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
└── runtime.txt

这是 heroku 输出的完整错误:

  -----> $ python manage.py collectstatic --noinput
         Traceback (most recent call last):
           File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 197, in fetch_command
             app_name = commands[subcommand]
         KeyError: 'collectstatic'
         During handling of the above exception, another exception occurred:
         Traceback (most recent call last):
           File "/tmp/build_b972a97c_/manage.py", line 15, in <module>
             execute_from_command_line(sys.argv)
           File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
             utility.execute()
           File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 365, in execute
             self.fetch_command(subcommand).run_from_argv(self.argv)
           File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 204, in fetch_command
             settings.INSTALLED_APPS
           File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 56, in __getattr__
             self._setup(name)
           File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 43, in _setup
             self._wrapped = Settings(settings_module)
           File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 125, in __init__
             raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
         django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
   !     Error while running '$ python manage.py collectstatic --noinput'.
         See traceback above for details.
         You may need to update application code to resolve this error.
         Or, you can disable collectstatic for this application:
            $ heroku config:set DISABLE_COLLECTSTATIC=1
         https://devcenter.heroku.com/articles/django-assets
  ****** Collectstatic environment variables:
         PYTHONUNBUFFERED=1
         PKG_CONFIG_PATH=/app/.heroku/vendor/lib/pkg-config:/app/.heroku/python/lib/pkg-config:
         DEBUG_COLLECTSTATIC=1
         BPLOG_PREFIX=buildpack.python
         PWD=/tmp/build_b972a97c_
         HOME=/app
         LANG=en_US.UTF-8
         SOURCE_VERSION=431de8bbd806ac08d344f95fccb4dfc362b9b9b3
         REQUEST_ID=ebfaedf0-3f8d-a9d8-d279-f1855c4e71e4
         ENV_DIR=/tmp/d20210210-48-1ahpoz1
         PYTHONPATH=.
         CPLUS_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:
         BIN_DIR=/tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136/bin
         LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:
         SHLVL=1
         LD_LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:
         PIP_NO_PYTHON_VERSION_WARNING=1
         BUILDPACK_LOG_FILE=/dev/fd/3
         STACK=heroku-20
         BUILD_DIR=/tmp/build_b972a97c_
         CACHE_DIR=/tmp/codon/tmp/cache
         PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin::/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136/vendor/
         EXPORT_PATH=/tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136/bin/../export
         C_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:
         DYNO=run.4886
         PROFILE_PATH=/tmp/build_b972a97c_/.profile.d/python.sh
         OLDPWD=/tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136
         _=/usr/bin/env
   !     Push rejected, failed to compile Python app.
   !     Push failed

我已经尝试在我的 settings.py 中应用它,但它似乎没有用:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'), // base_dir/static
)

一种理论与我的错误部分有关,其中提到“django.core.exceptions.ImproperlyConfigured:SECRET_KEY 设置不能为空。”也许我搞砸了我的 env 变量,所以为了更好地衡量,我将展示如何设置我的 .env 文件。 .env 文件:

SECRET_KEY = '*********************************'

我想知道为什么它没有在本地显示错误,所以可能有什么东西没有正确推送,因为它在我的 .gitignore?

这是我的 gitignore:

# Ignore These Files
.env
**/.DS_Store
*.log
*.pyc
__pycache__/
*.py[cod]
*$py.class
local_settings.py
db.sqlite3
db.sqlite3-journal

# Ignore Django Migrations in Development if you are working on team

# Only for Development only
# **/migrations/**
# !**/migrations
# !**/migrations/__init__.py

谢谢 Ankit Tiwari。当我添加一个带有密钥 SECRET_KEY 和我的密钥值的 Config Var 时,我的问题得到解决。为 SECRET_KEY 的每个实例使用唯一键值可能是最佳做法。我还看到了某些自动生成键值的设置。

如果您想制作第二个或新的 SECRET_KEY,请使用此代码:

from django.core.management.utils import get_random_secret_key

print(get_random_secret_key())