我似乎无法让Django从环境变量中读取我配置的设置。我在线跟踪了一些指南,并发现了其他一些问题,因此尝试了如下配置:
Apache配置:
WSGIScriptAlias "/v4" /usr/local/myproject4/myproject4/wsgi.py
WSGIPythonPath /usr/local/myproject4:/usr/local/myproject4/env/lib/python2.7/site-packages
<VirtualHost *:8000>
SetEnv MYPROJECT_SECRET_KEY 'xxx'
SetEnv MYPROJECT_DB_USER 'xxxx'
SetEnv MYPROJECT_DB_PASS 'xxxx'
<Directory /usr/local/myproject4/myproject4>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
我的wsgi.py文件看起来包含此内容(以检索设置):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject4.settings")
from django.core.handlers.wsgi import WSGIHandler
_application = WSGIHandler()
def application(environ, start_response):
for key, value in environ:
if key.startswith('MYPROJECT_'):
os.environ[key] = value;
return _application(environ, start_response)
但是每当我尝试检索设置时,我都会这样:
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21912): Target WSGI script '/usr/local/myproject4/myproject4/wsgi.py' cannot be loaded as Python module.
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21912): Exception occurred processing WSGI script '/usr/local/myproject4/myproject4/wsgi.py'.
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/myproject4/wsgi.py", line 14, in <module>
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] from django.core.handlers.wsgi import WSGIHandler
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 11, in <module>
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] from django.core.handlers import base
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 12, in <module>
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] from django.db import connections, transaction
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/db/__init__.py", line 83, in <module>
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] signals.request_started.connect(reset_queries)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] if settings.DEBUG:
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] self._setup(name)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] self._wrapped = Settings(settings_module)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] mod = importlib.import_module(self.SETTINGS_MODULE)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] __import__(name)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/myproject4/settings.py", line 29, in <module>
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] SECRET_KEY = get_env_variable('MYPROJECT_SECRET_KEY')
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] File "/usr/local/myproject4/myproject4/settings.py", line 23, in get_env_variable
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] raise ImproperlyConfigured(error_msg)
[Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] ImproperlyConfigured: Set the MYPROJECT_SECRET_KEY environment variable
真的很感激,如果有人能帮我辨别我做错了什么。
答案 0 :(得分:2)
我需要相同的功能来处理prod / dev环境......并找到以下文章: http://drumcoder.co.uk/blog/2010/nov/12/apache-environment-variables-and-mod_wsgi/
我刚尝试过,它立即起作用了。请注意以下划线为前缀的处理程序名称:
_application = django.core.handlers.wsgi.WSGIHandler()