bash 你如何将环境变量添加到你的 django 项目中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37473108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add environment variables to your django project
提问by nothingness
I'm trying to set up my project so that it can use environment variables locally
我正在尝试设置我的项目,以便它可以在本地使用环境变量
Ive tried adding it to the end of my activate file and a list of other things. I'm trying to use this
我试过将它添加到我的激活文件和其他内容列表的末尾。我正在尝试使用这个
from .base import *
if os.environ['DJANGO_SERVER_TYPE'] == 'local':
try:
from .local import *
except:
pass
if os.environ['DJANGO_SERVER_TYPE'] == 'production':
try:
from .production import *
except:
pass
I am a real novice and often things are explained brief and matter of factly. So a thorough explanation would really be beneficial to me in how to implement this thanks. I've NEVER done anything n Bash. Ive tried doing this
我是一个真正的新手,经常对事情进行简短的解释和实事求是。因此,在如何实施这一感谢方面,彻底的解释真的对我有益。我从来没有在 Bash 上做过任何事情。我试过这样做
export KEY=VALUE
in the activate file, only for it not to be recognized by the system, and I had to remove it in order to use my local server
在激活文件中,只是因为它不被系统识别,我不得不删除它才能使用我的本地服务器
采纳答案by tennismogul
If you're running it through the Django web server, you can pass environment variables the same way you would to any other command:
如果您通过 Django Web 服务器运行它,您可以像传递任何其他命令一样传递环境变量:
DJANGO_SERVER_TYPE="local" ./manage.py runserver
If you're running it through a web server like Apache, you can set environment variables through your virtual host configuration:
如果你通过像 Apache 这样的 web 服务器运行它,你可以通过你的虚拟主机配置设置环境变量:
SetEnv DJANGO_SERVER_TYPE local
回答by kojiro
I have a settings module that contains something like the following:
我有一个包含以下内容的设置模块:
…
import os
from django.core.exceptions import ImproperlyConfigured
…
def _require_env(name):
"""Raise an error if the environment variable isn't defined"""
value = os.getenv(name)
if value is None:
raise ImproperlyConfigured('Required environment variable "{}" is not set.'.format(name))
return value
…
SECRET_KEY = _require_env('SOMETHING_SECRET_KEY')
_db_host = os.getenv('SOMETHING_MYSQL_HOST', 'mysql')
_db_port = os.getenv('SOMETHING_MYSQL_PORT', 3306)
_db_name = _require_env('SOMETHING_MYSQL_DATABASE')
_db_user = _require_env('SOMETHING_MYSQL_USER')
_db_password = _require_env('SOMETHING_MYSQL_PASSWORD')
DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql',
'NAME': _db_name,
'HOST': _db_host,
'PORT': _db_port,
'USER': _db_user,
'PASSWORD': _db_password,
… } }
and so on.
等等。
_require_env
is for environment variables that mustbe set. If those environment values are not found, Django immediately raises an ImproperlyConfigured
error. In other cases I just use os.getenv
with a default value.
_require_env
用于必须设置的环境变量。如果找不到这些环境值,Django 会立即引发ImproperlyConfigured
错误。在其他情况下,我只使用os.getenv
默认值。
回答by jauyeung
Install the environ library and add the following code to your settings file:
安装环境库并将以下代码添加到您的设置文件中:
root_path = environ.Path(__file__) - 2
env = environ.Env(DEBUG=(bool, False), DJANGO_ENV=(str, 'dev')) # set default values and casting
environ.Env.read_env(root_path('.env'))
Add a file called .env on the root of your project folder with variables formatted like:
在项目文件夹的根目录中添加一个名为 .env 的文件,其中的变量格式如下:
DEBUG=on