" os.environ"在Django settings.py不能得到系统环境变量与Apache和WSGI [英] "os.environ" in django settings.py cannot get system environment variables with apache and wsgi

查看:374
本文介绍了" os.environ"在Django settings.py不能得到系统环境变量与Apache和WSGI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成立了Django的setting.py是这样的:

I set up the django setting.py like this:

import os
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

环境变量被正确的ConfigEd。当内置的Web服务器Django的工作,一切正常。但随着Apache和WSGI工作时,提出了一个KeyError异常。

The environment variables were configed correctly. When working with the django built in web server, everything was ok. But working with apache and wsgi, it raised an KeyError.

据<一个href=\"http://stackoverflow.com/questions/16395491/cannot-get-environment-variables-in-django-settings-file\">Cannot获得在Django设置文件环境变量,问题就解决了​​。但为什么不是在Apache得到系统的环境变量?

According to Cannot get environment variables in Django settings file, the problem is solved. But why cannot apache get the system environment variables?

更新:
环境变量在.bashrc中设置。

UPDATED: The environment variables were set in .bashrc.

推荐答案

您说的环境变量在.bashrc中设置的。 presumably你的意思的的.bashrc中。这是没有意义的,因为Apache没有运行如你​​,正在运行的Apache用户。

You say "The environment variables were set in .bashrc." Presumably you mean your .bashrc. Which is pointless, because Apache is not running as you, it is running as the Apache user.

由于在您链接到非常问题所引用的博客文章解释说,需要设置环境变量通过 SETENV 指令Apache配置文件本身

As explained in the blog post referenced in the very question you link to, you need to set the environment variables in the Apache configuration file itself via the SetEnv directive.

这篇关于&QUOT; os.environ&QUOT;在Django settings.py不能得到系统环境变量与Apache和WSGI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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