Charles Haas Charles Haas - 1 month ago 8
Python Question

SECRET_KEY error when pushing django project to heroku

I am trying to push a django project that works well locally (with heroku local) to heroku. I my settings.py, I have a SECRET_KEY set, randomly created by django with startproject.

In both manage.py and wsgi.py, I have :

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")


This is my project's arborescence:

mysite/
├── db.sqlite3
├── lecture
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── media
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20161021_0952.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   ├── 0001_initial.cpython-34.pyc
│   │   ├── 0002_auto_20161021_0952.cpython-34.pyc
│   │   └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-34.pyc
│   │   ├── apps.cpython-34.pyc
│   │   ├── __init__.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── static
│   │   └── lecture
│   │   ├── images
│   │   │   └── background.gif
│   │   └── style.css
│   ├── templates
│   │   └── lecture
│   │   ├── ajouter.html
│   │   ├── base_site.html
│   │   ├── debug.html
│   │   ├── detail.html
│   │   ├── detail_oeuvre.html
│   │   ├── index.html
│   │   ├── index_lectures.html
│   │   └── results.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── log
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── media
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-34.pyc
│   │   ├── apps.cpython-34.pyc
│   │   ├── __init__.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── templates
│   │   └── log
│   │   └── login_form.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── middleware.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── polls
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── apps.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── media
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   └── __pycache__
│   │   ├── 0001_initial.cpython-34.pyc
│   │   └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── __pycache__
│   │   ├── admin.cpython-34.pyc
│   │   ├── apps.cpython-34.pyc
│   │   ├── __init__.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   ├── tests.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── static
│   │   └── polls
│   │   ├── images
│   │   │   └── background.gif
│   │   └── style.css
│   ├── templates
│   │   └── polls
│   │   ├── detail.html
│   │   ├── index.html
│   │   └── results.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── Procfile
├── requirements.txt
├── templates
│   └── admin
│   ├── base_site.html
│   └── index.html


When I run git push heroku master, I get an ImproperlyConfigured error ; the SECRET_KEY setting must not be empty:

Counting objects: 116, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (105/105), done.
Writing objects: 100% (116/116), 1.38 MiB | 46.00 KiB/s, done.
Total 116 (delta 13), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-2.7.12
remote: $ pip install -r requirements.txt
remote: Collecting dj-database-url==0.4.1 (from -r requirements.txt (line 1))
remote: Downloading dj-database-url-0.4.1.tar.gz
remote: Collecting Django==1.10.1 (from -r requirements.txt (line 2))
remote: Downloading Django-1.10.1-py2.py3-none-any.whl (6.8MB)
remote: Collecting django-extensions==1.7.4 (from -r requirements.txt (line 3))
remote: Downloading django_extensions-1.7.4-py2.py3-none-any.whl (200kB)
remote: Collecting gunicorn==19.6.0 (from -r requirements.txt (line 4))
remote: Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
remote: Collecting six==1.10.0 (from -r requirements.txt (line 5))
remote: Downloading six-1.10.0-py2.py3-none-any.whl
remote: Collecting whitenoise==3.2.2 (from -r requirements.txt (line 6))
remote: Downloading whitenoise-3.2.2-py2.py3-none-any.whl
remote: Installing collected packages: dj-database-url, Django, six, django-extensions, gunicorn, whitenoise
remote: Running setup.py install for dj-database-url: started
remote: Running setup.py install for dj-database-url: finished with status 'done'
remote: Successfully installed Django-1.10.1 dj-database-url-0.4.1 django-extensions-1.7.4 gunicorn-19.6.0 six-1.10.0 whitenoise-3.2.2
remote:
remote: $ python manage.py collectstatic --noinput
remote: Traceback (most recent call last):
remote: File "manage.py", line 22, in <module>
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 196, in fetch_command
remote: settings.INSTALLED_APPS
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
remote: self._setup(name)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
remote: self._wrapped = Settings(settings_module)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
remote: mod = importlib.import_module(self.SETTINGS_MODULE)
remote: File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
remote: __import__(name)
remote: File "/app/mysite/urls.py", line 20, in <module>
remote: url(r'^polls/', include('polls.urls')),
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
remote: urlconf_module = import_module(urlconf_module)
remote: File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
remote: __import__(name)
remote: File "/app/polls/urls.py", line 3, in <module>
remote: from . import views
remote: File "/app/polls/views.py", line 5, in <module>
remote: from .models import Choice, Question
remote: File "/app/polls/models.py", line 9, in <module>
remote: class Question(models.Model):
remote: File "/app/polls/models.py", line 10, in Question
remote: question_text = models.CharField(max_length = 200)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1043, in __init__
remote: super(CharField, self).__init__(*args, **kwargs)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
remote: self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
remote: self._setup(name)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
remote: self._wrapped = Settings(settings_module)
remote: File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 116, in __init__
remote: raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
remote: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
remote:
remote: You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote: https://devcenter.heroku.com/articles/django-assets
remote: ! Push rejected, failed to compile Python app.
remote:
remote: ! Push failed
remote: Verifying deploy....
remote:
remote: ! Push rejected to lecturapp.
remote:
To https://git.heroku.com/lecturapp.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lecturapp.git'


I simply can't understand the problem. When I look it up, all I find are questions from people who did not set SECRET_KEY, but mine is set in settings.py.

Thanks in advance for your help!

EDIT: Here are my Procfile :

web: gunicorn mysite.wsgi --log-file -


And my wsgi.py:

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)




os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

Answer

You are able to set environment variables in Heroku (under the tab settings iirc). Please add your secret_key there. Up next add DISABLE_COLLECTSTATIC=1 as well.