I've looked through a bunch of other StackOverflow and forum pages with very similar issues, but none of their solutions worked.
The error is caused during the python manage.py collectstatic --noinput
test command when you deploy to heroku. Running python manage.py collectstatic --noinput
on my local project works without errors.
Here is my 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
my 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())
Here is my file tree:
.
├── 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
This is the full error that heroku outputs:
-----> $ 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
I've tried applying this in my settings.py
, but it didn't seem to work:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'), // base_dir/static
)
One theory is something to do with the part of my error that mentions "django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty." Maybe I messed up my env variable, so for good measure I'll show how I setup my .env file. The .env file:
SECRET_KEY = '*********************************'
I was wondering why it didn't show an error locally, so maybe something isn't pushing correctly because it's in my .gitignore
?
Here is my 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
Thanks Ankit Tiwari. My problems were fixed when I added a Config Var
with the key SECRET_KEY
and a value of my secret key. It's likely best practice to use a unique key value for each instance of SECRET_KEY
. I've Also seen certain setups that autogenerate key values.
If you'd like to make a second or new SECRET_KEY
use this code:
from django.core.management.utils import get_random_secret_key
print(get_random_secret_key())