简体   繁体   中英

“os.environ” in django settings.py cannot get system environment variables with apache and wsgi

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)

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.

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

UPDATED: The environment variables were set in .bashrc.

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.

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.

您在settings.py中设置的任何环境将仅应用于该实例的环境变量,它们不会更改将被称为apache的系统env变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM