bash 我在哪里为 Django 设置环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44693485/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:13:48  来源:igfitidea点击:

Where do I set environment variables for Django?

djangobashubuntuenvironment-variables

提问by jayuloy

everyone!

每个人!

Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04 in AWS

AWS 中的 Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04

I want to set environment variables for sensitive info.(django secret key, DB password...)

我想为敏感信息设置环境变量。(django 密钥,数据库密码...)

I studied many articles about setting ways.

我研究了很多关于设置方式的文章。

But when I tried os.environ['env_name'],

但是当我尝试 os.environ['env_name'] 时,

  1. .bashrc: Not working

  2. .bash_profile: Not working

  3. .profile: Not working

  4. /etc/environment: Not working

  5. Gunicorn script file.(systemd): I set them in gunicorn systemd script. It work very well.

  1. .bashrc:不工作

  2. .bash_profile: 不工作

  3. .profile:不工作

  4. /etc/environment: 不工作

  5. Gunicorn 脚本文件。(systemd):我在 gunicorn systemd 脚本中设置它们。它工作得很好。

But because I want to use the environment variables in other program too, I set them among 1~5 configurations. I don't understand why 1~5 configurations didn't work. Is there scope or priority of setting environment variables?

但是因为我也想在其他程序中使用环境变量,所以我将它们设置在1~5个配置中。我不明白为什么 1~5 个配置不起作用。是否有设置环境变量的范围或优先级?

EDIT:

编辑:

I use Ubuntu 16.04 server. I can't restart terminal session.

我使用 Ubuntu 16.04 服务器。我无法重新启动终端会话。

I tried 'source .bashrc' and logout/login. But It didn't work.

我试过'source .bashrc' 和注销/登录。但它没有用。

Of cource, 'echo $some_env_var' is working, I say, django can't read.

当然,'echo $some_env_var' 正在工作,我说,django 无法读取。

采纳答案by Exprator

create a file called .bashrc in your server

在您的服务器中创建一个名为 .bashrc 的文件

export('the_name_in_bashrc', some_value)

then in the settings.py

然后在 settings.py

import os
some_variable = os.environ.get('the_name_in_bashrc')

回答by Braden Holt

.bashrc will work for local development but not for a production environment. I just spent quite a bit of time looking for the answer to this and here's what worked for me:

.bashrc 将适用于本地开发,但不适用于生产环境。我只是花了很多时间寻找这个问题的答案,这对我有用:

1) Create a file somewhere on your server titled settings.ini. I did this in /etc/project/settings.ini

1) 在您的服务器上的某处创建一个名为 settings.ini 的文件。我在 /etc/project/settings.ini 中做了这个

2) Add your config data to that file using the following format where the key could be an environmental variable and the value is a string. Note that you don't need to surround the value in quotes.

2) 使用以下格式将您的配置数据添加到该文件中,其中键可以是环境变量,值是字符串。请注意,您不需要将值括在引号中。

[section]
secret_key_a=somestringa
secret_key_b=somestringb

3) Access these variables using python's configparserlibrary. The code below could be in your django project's settings file.

3)使用python的configparser库访问这些变量。下面的代码可能在您的 django 项目的设置文件中。

from configparser import RawConfigParser

config = RawConfigParser()
config.read('/etc/project/settings.ini')

DJANGO_SECRET = config.get('section', 'secret_key_a')

Source: https://code.djangoproject.com/wiki/SplitSettings(ini-style section)

来源:https: //code.djangoproject.com/wiki/SplitSettings(ini 样式部分)

回答by Josh

If you're using a virtual ENV you can add the environment variables to that specific environment. You can use export KEY=VALUEin your terminal but that will not persist. If you would like your values to persist you can edit the file:

如果您使用的是虚拟 ENV,则可以将环境变量添加到该特定环境中。您可以export KEY=VALUE在终端中使用,但这不会持续存在。如果您希望您的值保持不变,您可以编辑该文件:

sudo nano your_environment/bin/activate

sudo nano your_environment/bin/activate

Then at the bottom add the values you want e.g.:

然后在底部添加您想要的值,例如:

export MY_KEY="12345"

export MY_KEY="12345"

And save. Remember to restart your ENV for changes to take effect.

并保存。请记住重新启动您的 ENV 以使更改生效。

回答by Andreas Bergstr?m

The simplest solution is as already mentioned using os.environ.get and then set your server environment variables in some way (config stores, bash files, etc.)

最简单的解决方案是使用 os.environ.get 然后以某种方式设置服务器环境变量(配置存储、bash 文件等)

Another slightly more sophisticated way is to use python-decoupleand .env files. Here's a quick how-to:

另一种稍微复杂的方法是使用python-decouple和 .env 文件。这是一个快速的操作方法:

1) Install python-decouple (preferably in a venv if not using Docker):

1)安装python-decouple(如果不使用Docker,最好在venv中):

pip install python-decouple

2) Create a .env file in the root of your Django-project, add a key like;

2) 在你的 Django 项目的根目录中创建一个 .env 文件,添加一个像;

SECRET_KEY=SomeSecretKeyHere

3) In your settings.py, or any other file where you want to use the configuration values:

3) 在 settings.py 或任何其他要使用配置值的文件中:

from decouple import config

...

SECRET_KEY = config('SECRET_KEY')

4) As you probably don't want these secrets to end up in your version control system, add the file to your .gitignore. To make it easier to setup a new project, you could have a .env_default checked into the VCS containing default/dummy-values that's not used in production.

4) 因为您可能不希望这些秘密最终出现在您的版本控制系统中,所以将该文件添加到您的 .gitignore 中。为了更轻松地设置新项目,您可以将 .env_default 签入包含未在生产中使用的默认值/虚拟值的 VCS。