Where do I set environment variables for Django?

时间:2022-03-11 11:32:30

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

    .bashrc:不行

  2. .bash_profile: Not working

    .bash_profile:不行

  3. .profile: Not working

    .profile:不行

  4. /etc/environment: Not working

    / etc / environment:不工作

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

    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'和logout / login。但它没有用。

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

对于cource,'echo $ some_env_var'正在工作,我说,django无法读取。

2 个解决方案

#1


3  

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')

#2


2  

.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 configparser library. 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-style section)

#1


3  

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')

#2


2  

.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 configparser library. 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-style section)