Han*_*pan 21 django heroku gunicorn
我跟着Heroku支持中心的Django入门指南,但是当我尝试在Heroku或工头上启动时,我收到以下错误:
ImportError: No module named wsgi
Run Code Online (Sandbox Code Playgroud)
这是我的procfile:
web: gunicorn testproject.wsgi -b 0.0.0.0:$PORT
Run Code Online (Sandbox Code Playgroud)
这是我的django项目设置文件:
from datetime import date, timedelta
import os
oneyr = date.today() + timedelta(days=365)
PROJECT_DIR = os.path.dirname(__file__)
DEBUG = os.environ.get('DEBUG')
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
TIME_ZONE = 'Europe/London'
LANGUAGE_CODE = 'en-gb'
USE_I18N = False
USE_L10N = True
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static/')
MEDIA_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static/'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'testproject.urls'
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, 'templates')
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gunicorn',
'storages',
)
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么这个应用程序不会启动?
Arp*_*Rai 19
这是一个古老的问题,但我正在回应它,因为我最近遇到了这个确切的问题,而Intenex的回答虽然有帮助但无法解决我的问题.
正如Intenex所指出的,你可能错过了这个wsgi.py文件.这可能是因为您使用Django 1.3或以前的版本创建了项目.使用Django 1.4创建项目会自动创建,wsgi.py就像manage.py,settings.py.
与Intenex指出的不同,这是你应该做的.假设您使用的是Django 1.3或更早版本,请wsgi.py在项目目录中创建文件(同一目录manage.py等)并将以下内容添加到您的wsgi.py文件中:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# This application object is used by the development server
# as well as any WSGI server configured to use this file.
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Run Code Online (Sandbox Code Playgroud)
请注意与Intenex的答案相比,最后两个语句的差异.
有了这个,您应该能够foreman start在Procfile中运行以下内容:
web: gunicorn mysite.wsgi
看起来您的项目目录中没有 wsgi.py 模块文件。如果您使用 Django 1.3 或更早版本启动项目(使用 django-admin.py startproject 项目),则不会自动创建 wsgi 文件。您需要按照本指南自行创建:
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/
根据指南,将其放入文件中,确保将“mysite”更改为您的项目名称:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# This application object is used by the development server
# as well as any WSGI server configured to use this file.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)
此外,由于我在您的设置中没有看到这一点,因此您需要确保您的项目目录包含在您的 Python 路径中,按照此处的最后一句:https : //docs.djangoproject.com/en/dev/操作方法/部署/wsgi/gunicorn/#running-django-in-gunicorn-as-a-generic-wsgi-application
否则你会遇到和我一样的问题: Python app import error in Django with WSGI gunicorn
在那之后,一切都应该顺利进行。干杯!
旁注:您可能已经可以通过将 Procfile 更改为以下内容来运行 gunicorn:
web: python manage.py run_gunicorn
Run Code Online (Sandbox Code Playgroud)
根据https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/#using-gunicorn-s-django-integration,但我自己没有测试过,所以不确定。可能仍然需要 wsgi.py 模块。
| 归档时间: |
|
| 查看次数: |
36255 次 |
| 最近记录: |