user504909
user504909

Reputation: 9549

how to import the custormed settings.py variable in django project?

I use customer settings in my django project as in my manage.py. I write:

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "setting.windows")
...

I want to run my program in different settings when I run my code like:

python manage.py runserver --settings=twoscoops.settings.local

In my settings.py I also add some my private program variable, different settings.py use different variables. If my models.py or other classes file want to import those variable defined in my setting file. How do I write the import Statement?

I mean if I do

import twoscops.settings.local

I may change the environment variable, when I use other settings. HOW to do it?

Upvotes: 2

Views: 1225

Answers (2)

orokusaki
orokusaki

Reputation: 57158

A good way to do this is actually to set an environment variable in your OS and then check for it in your Python code from within an environment module (as shown below).

Note: use whatever works for you, in terms of directory structure - I use <my app>/conf/envs/<some env>.py for environment-specific settings

# twoscoops/conf/envs/current.py
"""
Imports the proper settings, based on the deployment environment's name (set
as an environment variable), defaulting to local settings.
"""
import os

from django.core.exceptions import ImproperlyConfigured


# Environments (these can be called anything you want)
ENV_LOCAL = 'local'
ENV_PROD = 'prod'
DEPLOYMENT_ENVS = (ENV_LOCAL, ENV_PROD)

# Get the deployment environment's name from the os environment
DEPLOYMENT_ENV = os.getenv('DJANGO_DEPLOYMENT_ENV', ENV_LOCAL)

# Ensure the deployment env's name is valid
if DEPLOYMENT_ENV not in DEPLOYMENT_ENVS:
    raise ImproperlyConfigured(
        u'Invalid `DJANGO_DEPLOYMENT_ENV`: {d}'.format(d=DEPLOYMENT_ENV)
    )

# Import env-specific settings

if DEPLOYMENT_ENV == ENV_LOCAL:
    # Local, native testing
    from twoscoops.conf.envs.local import *

if DEPLOYMENT_ENV == ENV_PROD:
    # Production
    from twoscoops.conf.envs.prod import *

Just add from twosscoops.conf.envs.current import * to each of your main settings modules (you might have just one, or you might have one for API, one for website, etc.).

You'll notice the above example defaults to twoscoops.conf.envs.local. When you want to use another env (in my example, there is just prod and local), just add the DJANGO_DEPLOYMENT_ENV environment variable before starting your shell or server (or in an Upstart script before launching uWSGI, etc.), like so:

you@your-server$ export DJANGO_DEPLOYMENT_ENV=prod
you@your-server$ python manage.py shell

In Windows, you can set an environment variable as well (I believe in the command line and also via a GUI - Google will know more).

Upvotes: 1

Craig Labenz
Craig Labenz

Reputation: 2555

You can also cheat a little bit for your local development and simply put export DJANGO_VARIABLE=value-of-variable at the end of your virtual environment's activate script.

Note that for production you will need to put something like this in your Apache vhost config (I'm sure it's something similar for nginx):

SetEnv DJANGO_VARIABLE value-of-variable

Note the lack of an equals sign between the variable name and value in the vhost config.

Upvotes: 0

Related Questions