我正在尝试在 Heroku 上部署一个带有 gunicorn 的 Django 应用程序,但遇到了一些问题。
当我开始我的项目时,我的 Django 版本是 1.3 并且不包含标准 wsgi.py 模块,所以我将标准 wsgi 模块添加为 top/wsgi.py(top 是我的项目名称,turk 是我的应用程序名称,topturk 是包含目录 - 保留因此错误日志在下面有意义)。
现在当我跑
gunicorn top.wsgi:application -b 0.0.0.0:$PORT
服务器成功启动,
19:00:42 web.1 | started with pid 7869
19:00:42 web.1 | 2012-07-25 19:00:42 [7869] [INFO] Starting gunicorn 0.14.5
19:00:42 web.1 | 2012-07-25 19:00:42 [7869] [INFO] Listening at: http://0.0.0.0:5000 (7869)
19:00:42 web.1 | 2012-07-25 19:00:42 [7869] [INFO] Using worker: sync
19:00:42 web.1 | 2012-07-25 19:00:42 [7870] [INFO] Booting worker with pid: 7870
但是当我导航到 0.0.0.0:5000 时,我得到一个内部服务器错误:
19:00:45 web.1 | 2012-07-25 17:00:45 [7870] [ERROR] Error handling request
19:00:45 web.1 | Traceback (most recent call last):
19:00:45 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
19:00:45 web.1 | respiter = self.wsgi(environ, resp.start_response)
19:00:45 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
19:00:45 web.1 | self.load_middleware()
19:00:45 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
19:00:45 web.1 | raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
19:00:45 web.1 | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
19:00:47 web.1 | 2012-07-25 17:00:47 [7870] [ERROR] Error handling request
19:00:47 web.1 | Traceback (most recent call last):
19:00:47 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
19:00:47 web.1 | respiter = self.wsgi(environ, resp.start_response)
19:00:47 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
19:00:47 web.1 | self.load_middleware()
19:00:47 web.1 | File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
19:00:47 web.1 | raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
19:00:47 web.1 | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
我假设这是一个 python 路径错误,服务器不知道如何从我的应用程序目录导入
相关的导入代码在设置中:
MIDDLEWARE_CLASSES = (
'turk.middleware.subdomain.SubdomainMiddleware',
'turk.middleware.removewww.RemoveWWWMiddleware',
)
我试图通过在我的 settings.py 文件顶部将我的应用程序目录插入到 sys.path 中来解决这个问题,如下所示:
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(1, PROJECT_ROOT+'/turk/')
我已经验证将应用程序目录添加到路径中,但仍然没有骰子。有任何想法吗?还
sys.path.insert(1, PROJECT_ROOT+'/turk/')
看起来很骇人,并在路径中添加了至少两个目录副本,在 Django 中附加到 PYTHON_PATH 的正确方法是什么?谢谢!