" os.environ"在django settings.py中无法使用apache和wsgi获取系统环境变量 [英] "os.environ" in django settings.py cannot get system environment variables with apache and 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)
正确配置环境变量。当使用内置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.
根据在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中设置。大概你的意思是你的 .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
指令。
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.
这篇关于" os.environ"在django settings.py中无法使用apache和wsgi获取系统环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!