Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" : /

like image 508
Alejandro Veintimilla Avatar asked Jun 05 '14 22:06

Alejandro Veintimilla


People also ask

What is Procfile Heroku Django?

First, and most importantly, Heroku web applications require a Procfile . This file is used to explicitly declare your application's process types and entry points. It is located in the root of your repository.

Can Heroku serve static files?

Clearly, static files (defaults to index. html) can't be served directly on Heroku. So we need to transform static web app into any of these supported web apps. The simplest alternative is to use Node.

What is Django_heroku?

This is a Django library for Heroku applications that ensures a seamless deployment and development experience. This library provides: Settings configuration (Static files / WhiteNoise). Logging configuration. Test runner (important for Heroku CI).

Can I use Django with Heroku?

When you deploy to Heroku, the dependencies you specify in your requirements. txt file are automatically installed before app startup. If you're using Django, the collectstatic command also runs automatically during the deployment process.


2 Answers

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 
like image 159
Terrence Pugh Avatar answered Sep 23 '22 15:09

Terrence Pugh


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
like image 45
Soytoise Avatar answered Sep 23 '22 15:09

Soytoise