2

我似乎無法讓Django讀取我從環境變量中配置的設置。我在網上跟着一些導遊,並發現了一些其他的問題,並因此已經嘗試如下配置:Django從apache獲取環境變量

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 

真的很感謝,如果有人能幫助我確定我做錯了什麼。

+1

好了,誤差在談論'myproject_SECRET_KEY',你好像並沒有把設置在Apache的conf。 –

+0

對不起,我修改了我的代碼中的變量名稱,試圖製作一個更簡單的問題(具有相反的效果)。我會編輯它。 –

回答