无法在 Django 设置文件中获取环境变量 [英] Cannot get environment variables in Django settings file

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

问题描述

我正在尝试读取 Django 设置中的一些环境变量,这些环境变量已在/home/user/.bashrc (以及后者在/etc/bash.bashrc )中定义,但我得到的只是 KeyError 异常.我知道我的环境变量已设置,因为我可以在终端中打印它们(echo $VAR_NAME).这应该是微不足道的.

I'm trying to read some environment variables in Django settings, which i have defined in /home/user/.bashrc (and latter in /etc/bash.bashrc ) , but all i get is a KeyError exception. I know my environment variables are set, because i can print them in the terminal (echo $VAR_NAME). This should be trivial.

这是我正在使用的代码.

This is the code i'm using.

from django.core.exceptions import ImproperlyConfigured

msg = "Set the %s environment variable"


def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = msg % var_name
        raise ImproperlyConfigured(error_msg)

OS_DB_USER = get_env_variable('MY_USER')
OS_DB_PASS = get_env_variable('MY_PASS')
OS_DB_DB = get_env_variable('MY_DB')
OS_GAME_LOGS = get_env_variable('DIR_LOGS')

我就是找不到丢失的东西.有什么建议吗?

I just can't find what's missing. Any suggestions out there?

谢谢

使用 mod_wsgi 在 Apache 上运行.

Running on Apache with mod_wsgi.

推荐答案

我已经通过使用这个解决方案解决了我的问题:

I've manage to solve my problem by using this solution:

http://drumcoder.co.uk/blog/2010/nov/12/apache-environment-variables-and-mod_wsgi/

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

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