将django项目推送到heroku时出现SECRET_KEY错误

时间:2016-10-30 10:45:13

标签: python django git heroku

我正在尝试将一个在本地(与heroku本地)很好地运行的django项目推送到heroku。我的settings.py,我有一个SECRET_KEY集,由django用startproject随机创建。

在manage.py和wsgi.py中,我有:

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

这是我的项目的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

当我运行 git push heroku master 时,我收到 ImproperlyConfigured 错误; SECRET_KEY设置不能为空

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'
我根本无法理解这个问题。当我查找时,我发现的所有内容都是来自没有设置SECRET_KEY的人的问题,但我的设置是在settings.py中。

提前感谢您的帮助!

编辑:这是我的Procfile:

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

我的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")

1 个答案:

答案 0 :(得分:0)

您可以在Heroku中设置环境变量(在标签设置iirc下)。请在那里添加您的secret_key。接下来还要添加DISABLE_COLLECTSTATIC=1