Domanda

I have created a production_settings.py in which I am putting all the production env variables and values eg:

import dj_database_url
DATABASES['default'] =  dj_database_url.config()

I thought I'd declare an env variable like

MYPROJECT_PRODUCTION

and set this like

heroku config:add MYPROJECT_PRODUCTION=True or export MYPROJECT_PRODUCTION=True

In the settings.py (which is the default created by django) ,I thought I'd add at the end of the file

import os
if os.environ.has_key('MYPROJECT_PRODUCTION') and os.environ.get('MYPROJECT_PRODUCTION')=='True':
    from production_settings import *

Is this the correct way of doing this?

I am getting an import error when I try python manage shell

export DJANGO_SETTINGS_MODULE='myproject.settings'
export MYPROJECT_PRODUCTION=True
me@ubuntu:~/dev/python/django/myproject$ python manage.py shell
Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)

the manage.py exists in the same folder as settings.py ..still the error occurs.

I checked echo $MYPROJECT_PRODUCTION which outputs True

È stato utile?

Soluzione

Personally, I keep my production settings in settings.py then include a local_settings.py file (that's excluded from revision control with .hgignore).

I add the following to the end of settings.py

try:
    from local_settings import *
except ImportError, e:
    pass

Then in my local_settings.py I override the appropriate settings -

DEBUG = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'tag',
        'USER': 'tag',
        'PASSWORD': 'tag',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

I'm under the impression that this is a fairly well used method (I picked it up from colleagues but I've seen it blogged about too)

EDIT

In response to balazs very good point you might include a variation on this method, in order to keep sensitive data private. Perhaps include the following after the local_settings import -

try:
    from production_settings import *
except ImportError, e:
    pass

Then exclude production_settings.py from version control. I guess you may need to use a different method to deploy production_settings.py, but I don't suppose that's too big a deal.

Altri suggerimenti

I advise against using different settings files for different environments in favour of customising your settings with environment variables. This allows you by default to use your local development settings and override it in production.

For example database and static / media root settings

# Default database URL is a local SQLite instance
DATABASE_URL = 'sqlite:///%s' % os.path.join(os.path.dirname(__file__), 'db.sqlite')

DATABASES = {
    'default': dj_database_url.config('DATABASE_URL', default=DATABASE_URL),
}

MEDIA_ROOT = os.environ.get('MEDIA_ROOT',
                            os.path.join(os.path.dirname(__file__), 'media'))

MEDIA_URL = os.environ.get('MEDIA_URL', '/media/')


STATIC_ROOT = os.environ.get('STATIC_ROOT',
                             os.path.join(os.path.dirname(__file__), 'static'))

STATIC_URL = os.environ.get('STATIC_URL', '/static/')

This allows you to set any setting on Heroku via heroku config:set and removes the complexity of dealing with multiple settings files, for example:

heroku config:set MEDIA_URL=http://custom-media-server.com/media/
heroku config:set STATIC_URL=http://custom-media-server.com/static/

I have also created a custom Django project template that can take most settings from environment variables.

You should also look at the 12 Factor Application website and specifically at how to store configuration.

Have you defined DATABASES as a dictionary? by:

DATABASES = {}

also show your heroku logs

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top