无法在Django中使用环境变量进行设置 [英] Cannot use environment variables for settings in Django

查看:259
本文介绍了无法在Django中使用环境变量进行设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图找到存储和保存settings.py和数据库之外的设置的地方时,我使用environment.json作为环境变量.我将它们导入settings.py.

我的问题是,当我尝试在环境中更改或存储新值时,envsettings.py不会注意到更改-可能是因为Django读取了settings.py的时间和次数./p>

有没有一种方法可以像下面尝试的那样使用我的环境变量?

# settings.py
import json
with open('/home/dotcloud/environment.json') as f:
    env = json.load(f)
EMAIL_HOST = env.get('EMAIL_PORT', '500')

# views.py
import json
def site_configuration(request):
    with open('/home/dotcloud/environment.json') as f:
        env = json.load(f)
    if request.method == 'POST':
        os.environ['EMAIL_PORT'] = request.POST['email_port']
    return render(request, ...)

# python manage.py shell demo
>>> import json
>>> with open('/home/dotcloud/environment.json') as f:
...     env = json.load(f)
... 
>>> project_settings.EMAIL_PORT
'500'
>>> env['EMAIL_PORT']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: 'EMAIL_PORT'
>>> env['EMAIL_PORT'] = "123"
>>> env['EMAIL_PORT']
'123'
>>> project_settings.EMAIL_PORT
'500'
>>> project_settings.EMAIL_PORT == env['EMAIL_PORT']
False'

如果没有,那么我还能如何存储由settings.py检索到的可变设置在我的Django项目中的某个地方?

解决方案

您可能想研究foreman( GitHub )或honcho( GitHub ).两者都在当前目录中寻找一个.env文件,从该文件中加载本地环境变量.

对于大多数项目,我的.env如下所示(我将dj-database-url用于数据库配置):

DATABASE_URL=sqlite://localhost/local.db
SECRET_KEY=<a secret key>
DEBUG=True

在您的settings.py文件中,您可以像这样从os.environ加载这些设置:

import os
DEBUG = os.environ.get('DEBUG', False)

如果有必需的设置,可以先assert它们的出现,然后再尝试设置它们:

assert 'SECRET_KEY' in os.environ, 'Set SECRET_KEY in your .env file!'
SECRET_KEY = os.environ['SECRET_KEY']

在我启动的最后几个项目中,我一直在使用这种方法来处理本地设置,我认为它确实很好用.一个警告是从不将您的.env提交给源代码管理.这些是仅适用于当前配置的本地设置,应在其他环境中重新创建.

In trying to find a place to store and save settings beyond settings.py and the database, I used an environment.json for environment variables. I import these in settings.py.

My problem is that when I try to change or store new values in my environment, env, settings.py does not notice the change - perhaps because the time and number of times settings.py is read by Django.

Is there a way I would be able to use my environment variables the way I want like attempted below?

# settings.py
import json
with open('/home/dotcloud/environment.json') as f:
    env = json.load(f)
EMAIL_HOST = env.get('EMAIL_PORT', '500')

# views.py
import json
def site_configuration(request):
    with open('/home/dotcloud/environment.json') as f:
        env = json.load(f)
    if request.method == 'POST':
        os.environ['EMAIL_PORT'] = request.POST['email_port']
    return render(request, ...)

# python manage.py shell demo
>>> import json
>>> with open('/home/dotcloud/environment.json') as f:
...     env = json.load(f)
... 
>>> project_settings.EMAIL_PORT
'500'
>>> env['EMAIL_PORT']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: 'EMAIL_PORT'
>>> env['EMAIL_PORT'] = "123"
>>> env['EMAIL_PORT']
'123'
>>> project_settings.EMAIL_PORT
'500'
>>> project_settings.EMAIL_PORT == env['EMAIL_PORT']
False'

And if not, how else could I store changeable settings that are retrieved by settings.py somewhere in my Django project?

解决方案

You might want to look into foreman (GitHub) or honcho (GitHub). Both of these look for a .env file in your current directory from which to load local environment variables.

My .env looks like this for most projects (I use dj-database-url for database configuration):

DATABASE_URL=sqlite://localhost/local.db
SECRET_KEY=<a secret key>
DEBUG=True

In your settings.py file, you can load these settings from os.environ like this:

import os
DEBUG = os.environ.get('DEBUG', False)

If there are required settings, you can assert their presence before trying to set them:

assert 'SECRET_KEY' in os.environ, 'Set SECRET_KEY in your .env file!'
SECRET_KEY = os.environ['SECRET_KEY']

I've been using this method of handling local settings for the last few projects I've started and I think it works really well. One caveat is to never commit your .env to source control. These are local settings that exist only for the current configuration and should be recreated for a different environment.

这篇关于无法在Django中使用环境变量进行设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆