【发布时间】:2021-05-13 20:05:58
【问题描述】:
我浏览了一堆其他 StackOverflow 和论坛页面,它们的问题非常相似,但他们的解决方案都没有奏效。
该错误是在部署到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
我的设置.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
【问题讨论】:
-
您是否在 heroku 上设置了
.env文件,因为您忽略了 gitignore 文件中的 .env 文件。
标签: python django heroku collectstatic