我在Elastic Beanstalk上成功部署了我的Django(1.7),但是当我在浏览器中加载它时,我得到500。在日志中我发现了这个:
[Wed Sep 24 12:56:11.434509 2014] [:error] [pid 27030] [remote 172.31.6.176:0] mod_wsgi (pid=27030): Target WSGI script '/opt/python/current/app/mysite/wsgi.py' cannot be loaded as Python module.
[Wed Sep 24 12:56:11.434557 2014] [:error] [pid 27030] [remote 172.31.6.176:0] mod_wsgi (pid=27030): Exception occurred processing WSGI script '/opt/python/current/app/mysite/wsgi.py'.
[Wed Sep 24 12:56:11.434596 2014] [:error] [pid 27030] [remote 172.31.6.176:0] Traceback (most recent call last):
[Wed Sep 24 12:56:11.434636 2014] [:error] [pid 27030] [remote 172.31.6.176:0] File "/opt/python/current/app/mysite/wsgi.py", line 14, in <module>
[Wed Sep 24 12:56:11.434698 2014] [:error] [pid 27030] [remote 172.31.6.176:0] application = get_wsgi_application()
[Wed Sep 24 12:56:11.434721 2014] [:error] [pid 27030] [remote 172.31.6.176:0] File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Wed Sep 24 12:56:11.434759 2014] [:error] [pid 27030] [remote 172.31.6.176:0] django.setup()
[Wed Sep 24 12:56:11.434781 2014] [:error] [pid 27030] [remote 172.31.6.176:0] File "/opt/python/run/venv/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
[Wed Sep 24 12:56:11.434813 2014] [:error] [pid 27030] [remote 172.31.6.176:0] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Wed Sep 24 12:56:11.434835 2014] [:error] [pid 27030] [remote 172.31.6.176:0] File "/opt/python/run/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
[Wed Sep 24 12:56:11.434867 2014] [:error] [pid 27030] [remote 172.31.6.176:0] self._setup(name)
[Wed Sep 24 12:56:11.434886 2014] [:error] [pid 27030] [remote 172.31.6.176:0] File "/opt/python/run/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
[Wed Sep 24 12:56:11.434916 2014] [:error] [pid 27030] [remote 172.31.6.176:0] % (desc, ENVIRONMENT_VARIABLE))
[Wed Sep 24 12:56:11.435013 2014] [:error] [pid 27030] [remote 172.31.6.176:0] ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我正在设置我的.ebextension mysite.config文件中的设置文件。
- option_name: DJANGO_SETTINGS_MODULE
value: "mysite.settings"
我还在大多数文件中添加了这个:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
问题出在我的设置文件应该是什么样子的地方。我已经用Google搜索了好几个小时了,但是我无法让它工作......我甚至不确定我的设置文件应该是什么样子。现在它看起来像这样:
LOGGING_CONFIG = 'django.utils.log.dictConfig'
我很确定这是设置文件的问题。如果有人能指出我正确的方向,我将非常感激。
答案 0 :(得分:1)
我还在大多数文件中添加了这个:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
这段代码只需要放在你的.wsgi文件中。它在其他地方没有用。
现在,一旦你完成了这一点,这就是为什么setdefault
方法不像手动分配一样做的事情:
关键问题是setdefault()方法在设置时的作用 环境变量DJANGO_SETTINGS_MODULE与使用相比 如前所述。在分配环境的情况下 变量总是更新。对于setdefault(),它只会更新 尚未设定。
通过this。希望这说清楚。
答案 1 :(得分:0)
所以在尝试了一切之后,我最终得到了加载示例Django项目。我更改了这一行:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
到这一行
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
在wsgi.py中。
为什么它现在有效?