简体   繁体   English

如何在django项目中导入custormed settings.py变量?

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

I use customer settings in my django project as in my manage.py. 我在Django项目中使用客户设置,就像在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. 在我的settings.py中,我还添加了一些我的私有程序变量,不同的settings.py使用了不同的变量。 If my models.py or other classes file want to import those variable defined in my setting file. 如果我的models.py或其他类文件要导入在我的设置文件中定义的那些变量。 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? 怎么做?

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). 一种有效的方法实际上是在操作系统中设置环境变量,然后在environment模块中的Python代码中检查它(如下所示)。

Note: use whatever works for you, in terms of directory structure - I use <my app>/conf/envs/<some env>.py for environment-specific settings 注意:在目录结构方面,请使用适合您的方法-我将<my app>/conf/envs/<some env>.py用于特定于环境的设置

# 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.). 只需from twosscoops.conf.envs.current import *添加到每个主要设置模块中(您可能只有一个,或者可能有一个用于API,一个用于网站,等等)。

You'll notice the above example defaults to twoscoops.conf.envs.local . 您会注意到上面的示例默认为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: 当您想使用另一个环境(在我的示例中,只有prod和local)时,只需在启动外壳程序或服务器之前(或在启动uWSGI等之前的Upstart脚本中)添加DJANGO_DEPLOYMENT_ENV环境变量,如下所示:

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). 在Windows中,您也可以设置环境变量(我相信在命令行中也可以通过GUI-Google会知道更多)。

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. 您也可以为本地开发作弊,只需将export DJANGO_VARIABLE=value-of-variable放在虚拟环境的activate脚本的末尾即可。

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): 请注意,在生产中,您需要在Apache vhost配置中放入类似的内容(我确定它与nginx类似):

SetEnv DJANGO_VARIABLE value-of-variable

Note the lack of an equals sign between the variable name and value in the vhost config. 请注意,vhost配置中的变量名称和值之间没有等号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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