3

我正在关注Heroku 上的 Django 入门教程
当我尝试使用 Foreman 在 Gunicorn 上运行 Django 应用程序时,我得到下一个错误回溯:

09:23:33 web.1  | started with pid 7012  
09:23:34 web.1  | 2013-05-06 09:23:34 [7012] [INFO] Starting gunicorn 0.17.2  
09:23:34 web.1  | 2013-05-06 09:23:34 [7012] [INFO] Listening at: `http://0.0.0.0:5000` (7012)  
09:23:34 web.1  | 2013-05-06 09:23:34 [7012] [INFO] Using worker: sync  
09:23:34 web.1  | 2013-05-06 09:23:34 [7015] [INFO] Booting worker with pid: 7015  
09:23:34 web.1  | 2013-05-06 09:23:34 [7015] [ERROR] Exception in worker process:  
09:23:34 web.1  | Traceback (most recent call last):  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 485, in spawn_worker  
09:23:34 web.1  |     worker.init_process()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process  
09:23:34 web.1  |     self.wsgi = self.app.wsgi()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 103, in wsgi  
09:23:34 web.1  |     self.callable = self.load()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 25, in load  
09:23:34 web.1  |     return util.import_app(self.app_uri)  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/util.py", line 372, in import_app  
09:23:34 web.1  |     __import__(module)  
09:23:34 web.1  |   File "/Users/cosmicMan66/DjangoDev/hepcat_server/hepcat_server/wsgi.py", line 27, in <module>  
09:23:34 web.1  |     from django.core.wsgi import get_wsgi_application  
09:23:34 web.1  | ImportError: No module named django.core.wsgi  
09:23:34 web.1  | Traceback (most recent call last):  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 485, in spawn_worker  
09:23:34 web.1  |     worker.init_process()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process  
09:23:34 web.1  |     self.wsgi = self.app.wsgi()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 103, in wsgi  
09:23:34 web.1  |     self.callable = self.load()  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 25, in load  
09:23:34 web.1  |     return util.import_app(self.app_uri)  
09:23:34 web.1  |   File "/usr/local/lib/python2.7/site-packages/gunicorn/util.py", line 372, in import_app  
09:23:34 web.1  |     __import__(module)  
09:23:34 web.1  |   File "/Users/cosmicMan66/DjangoDev/hepcat_server/hepcat_server/wsgi.py", line 27, in <module>  
09:23:34 web.1  |     from django.core.wsgi import get_wsgi_application  
09:23:34 web.1  | ImportError: No module named django.core.wsgi  
09:23:34 web.1  | 2013-05-06 09:23:34 [7015] [INFO] Worker exiting (pid: 7015)  
09:23:35 web.1  | 2013-05-06 09:23:35 [7012] [INFO] Shutting down: Master  
09:23:35 web.1  | 2013-05-06 09:23:35 [7012] [INFO] Reason: Worker failed to boot.  
09:23:35 web.1  | exited with code 3  
09:23:35 system | sending SIGTERM to all processes  
SIGTERM received

Procfile 位于项目的根目录中,包含:

网站:gunicorn hepcat_server.wsgi

settings.py 位于 hepcat_server 目录

当我使用$ python manage.py run_gunicorn
gunicorn 成功启动时,我看到了默认的 Django 页面

4

1 回答 1

1

在关注您使用的 Heroku 设置页面时,我遇到了同样的问题。我在此页面上找到了解决方案。我已将其复制到这里以供后代使用:

在 django 项目的根目录中,创建 Procfile 文件。然后在网上写下这个:sh -c "cd djangoproject && gunicorn djangoproject.wsgi"。然后在同一位置创建另一个名为 .env 的文件,将 DJANGO_SETTINGS_MODULE=djangoproject.settings 写入其中,这将帮助您设置无法在 djangoproject.wsgi 模块中设置的所需环境变量。运行 foreman start 以进行测试。给出下面的问题,这应该是不言自明的。

部署时,在 heroku 中创建应用程序后,使用命令 heroku config:set DJANGO_SETTINGS_MODULE=djangoproject.settings 设置环境变量,这是因为在文件 djangoproject/djangoproject/wsgi.py 运行之前部署失败。

请记住将“djangoproject”替换为您的 django 项目的名称。

于 2015-04-15T01:39:41.927 回答