Django Environment Variables

If you look at modern code deployment practices, like in the 12 factor
app
, environment variables are very important for keeping secret information, or specific information set for a server. All while not having long settings in a settings file —files, or every single server and host. It helps keep things simple and clean in your code along with the following quoted from the 12factorapp site:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

What Are Environment Variables?

Environment variables are a key:value pairs that can affect how a program runs.
They need to be set at some point before a process is run so the process can read them in and act accordingly. A lot of times in production environments your database name and passwords are set as environment variables so that information does not end up in a code repository somewhere. Unfortunately it do es even publicly sometimes.

By relying on an environment variable your code doesn’t care what your settings are because you can set them some other point. Actually working with environment variables is really simple. All you need to start is importing native os module:
import os.

<span>import</span> <span>os</span>
<span>name</span> <span>=</span> <span>os</span><span>.</span><span>environ</span><span>.</span><span>get</span><span>(</span><span>'USER'</span><span>)</span>
<span>import</span> <span>os</span>

<span>name</span> <span>=</span> <span>os</span><span>.</span><span>environ</span><span>.</span><span>get</span><span>(</span><span>'USER'</span><span>)</span>
import os name = os.environ.get('USER')

Enter fullscreen mode Exit fullscreen mode

OK but where to put and find these variables in your os? There are a
lot of ways but below 2 I think have best implementations:

1 – Virtual Environment

One of the best ways is to get them from virtual environment. When you activate your virtual env source .venv/bin/activate, “activate” script runs. You can add below script to the end of this activate file:

<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.env | xargs<span>)</span>
<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.env | xargs<span>)</span>
export $(grep -v '^#' ./.env | xargs)

Enter fullscreen mode Exit fullscreen mode

Where ./ is your project “root directory”. So if you have more than one env files you can add them too as below:

<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.env | xargs<span>)</span>
<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.envs/.env.dev | xargs<span>)</span>
<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.env | xargs<span>)</span>
<span>export</span> <span>$(</span><span>grep</span> <span>-v</span> <span>'^#'</span> ./.envs/.env.dev | xargs<span>)</span>
export $(grep -v '^#' ./.env | xargs) export $(grep -v '^#' ./.envs/.env.dev | xargs)

Enter fullscreen mode Exit fullscreen mode

I usually use this method but this requires an Unix-like OS (MacOS & Linux etc).

2 – Django-Environ Module Library

django-environ module allows you to use Twelve-factor
methodology
to configure your Django application with
environment variables.

In your project settings.py

<span>import</span> <span>environ</span>
<span>env</span> <span>=</span> <span>environ</span><span>.</span><span>Env</span><span>()</span>
<span># reading .env file </span><span>env</span><span>.</span><span>read_env</span><span>(</span><span>env</span><span>.</span><span>str</span><span>(</span><span>'./'</span><span>,</span> <span>'.env'</span><span>))</span>
<span># https://docs.djangoproject.com/en/dev/ref/settings/#secret-key </span><span>SECRET_KEY</span> <span>=</span> <span>env</span><span>(</span><span>'DJANGO_SECRET_KEY'</span><span>)</span>
<span>import</span> <span>environ</span>

<span>env</span> <span>=</span> <span>environ</span><span>.</span><span>Env</span><span>()</span>

<span># reading .env file </span><span>env</span><span>.</span><span>read_env</span><span>(</span><span>env</span><span>.</span><span>str</span><span>(</span><span>'./'</span><span>,</span> <span>'.env'</span><span>))</span>

<span># https://docs.djangoproject.com/en/dev/ref/settings/#secret-key </span><span>SECRET_KEY</span> <span>=</span> <span>env</span><span>(</span><span>'DJANGO_SECRET_KEY'</span><span>)</span>
import environ env = environ.Env() # reading .env file env.read_env(env.str('./', '.env')) # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env('DJANGO_SECRET_KEY')

Enter fullscreen mode Exit fullscreen mode

In addtion to all of these above you can export your env vars via .bashrc or .bash_profile or even .profile but this would be a bad practice: using project wide restrictions over system wide.

原文链接:Django Environment Variables

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
There are two ways of spreading light: to be the candle or the mirror that reflects it.
传递光亮有两种方式:成为一支蜡烛或当一面镜子
评论 抢沙发

请登录后发表评论

    暂无评论内容