简体   繁体   中英

Django. Using multiple settings files with Heroku

I am trying to follow the advice of the book "Two Scoops of Django" and although it is a really good book, I think it this section is unclear. So, I split my settings file and created a folder like this:

settings/
 __init__.py
 base.py (allmost everything there)
 local.py (dev. specific settings)
 production.py (settings for Heroku)

most of the settings are in the base.py file

in local.py I have this:

# settings/local.py
from .base import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INSTALLED_APPS += ("debug_toolbar", "django_extensions", "south",)

in production.py I have this:

from .base import *
INSTALLED_APPS += ("gunicorn",)

When I run locally:

python manage.py runserver 7000 --settings=appname.settings.local
python manage.py runserver 7000 --settings=appname.settings.production

everything works fine.

But when I push changes to Heroku, I get the log:

  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app

 raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

So, I guess Heroku is not finding my settings files, I don't know how to fix this (it might be very simple).

Two Scoops of Django is kind of ironic here, it writes "Platform as Service - See section 25.2" and then in that section it just writes "read Platform Documentation" : /

After you have logged into heroku with heroku login you can check your configs by running: heroku config . If you dont see a SECRET_KEY and DJANGO_SETTINGS_MODULE you can set them by running:

heroku config:set SECRET_KEY='secret_key_goes_here'

and

heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings.production

Finally, make sure that you have the following syntax inside of your production setting file:

SECRET_KEY = os.environ['SECRET_KEY']

The above intstructions are for the following project structure

-myproject
  -app1
  -app2
  -mysite
    -settings
      __init__.py
      base.py
      dev.py
      production.py
-manage.py
-Pipfile
-Procfile
-requirements.txt

You can use the environment variable DJANGO_SETTINGS_MODULE to specify a default settings module:

https://docs.djangoproject.com/en/dev/topics/settings/#envvar-DJANGO_SETTINGS_MODULE

On local Linux machine:

export DJANGO_SETTINGS_MODULE=settings.local

On Heroku:

heroku config:set DJANGO_SETTINGS_MODULE=settings.production

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM