【发布时间】:2013-05-23 11:06:08
【问题描述】:
我这样设置 django setting.py:
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)
环境变量配置正确。使用 django 内置 Web 服务器时,一切正常。但是使用 apache 和 wsgi,它引发了 KeyError。
根据Cannot get environment variables in Django settings file,问题解决了。但是为什么apache不能获取系统环境变量呢?
更新: 环境变量在 .bashrc 中设置。
【问题讨论】:
-
你是如何设置这些变量的?不要忘记 Apache 将以自己的用户身份运行。
-
“配置正确”是指使用
SetEnv指令在Apache 配置中设置它们? IIRC,Apache 清除环境,所以你需要有指令来公开你需要的环境变量。
标签: python django apache environment-variables wsgi