尝试在 Heroku 中打开 Django 应用程序时出现 ModuleNotFound 错误 - 使用 gunicorn 设置问题?

时间:2021-06-27 13:26:29

标签: django heroku gunicorn

我一直在构建我的第一个 Django 项目(到目前为止只是使用 runserver 在本地运行它),并且我正在采取措施将它托管在 Heroku 上,并添加了 gunicorn。构建成功,但是当我尝试打开应用程序时,Heroku 日志在工作进程中显示异常

ModuleNotFoundError: No module named 'mysite.wsgi'

最初我的 Procfile 有这个:

web: gunicorn mysite.wsgi

当我在本地尝试 gunicorn 命令时,它会在 family-django/mysite 目录中工作,但是当我从根目录 (family-django) 尝试时,它会给我相同的“无模块名为 mysite” .wsgi' 错误。根据 this post,Heroku 会从根目录尝试,所以我更新了我的 Procfile,如下所示,告诉它从 mysite 目录运行:

web: gunicorn --chdir mysite mysite.wsgi

这个新的 gunicorn 命令在从根目录 (family-django) 运行时确实可以在本地工作,万岁!!那一定是我需要的修复。 但是:在 Heroku 中更新 Procfile 并尝试再次打开应用程序后,它仍然失败,上面粘贴了“没有名为 mysite.wsgi 的模块”错误。 所以 Heroku 需要一些其他的调整来解决这个问题跑到那里去。

我的Django项目结构是这样的:

family-django
|-mysite 
| |-familytree
| |-myauth
| |-mysite
|   |-asgi.py
|   |-settings.py
|   |-urls.py
|   |-wsgi.py
|-Procfile
|-requirements.txt

wsgi.py 和 asgi.py 都是在项目开始时创建的。 (不确定同时拥有两者是否错误?)。 wsgi.py 有:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_wsgi_application()

settings.py 包含 INSTALLED_APPS 和 WSGI_APPLICATION 的以下信息:

    INSTALLED_APPS = [
    'familytree.apps.FamilytreeConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    "myauth",
]

WSGI_APPLICATION = 'mysite.wsgi.application'

既然我想在 Heroku 上解决这个问题,他们推荐使用 gunicorn,所以我添加了一个 requirements.txt:

django
django-heroku
gunicorn

Procfile 有:

web: gunicorn --chdir mysite mysite.wsgi

1 个答案:

答案 0 :(得分:0)

#----------------------------Install packages---------------------------- 
 1)pip install django-heroku
 2)pip install whitenoise

#-----------------------------setting.py----------------------------------#
1)INSTALLED_APPS = [
...,
'django_heroku',
]

2)MIDDLEWARE = [
  'whitenoise.middleware.WhiteNoiseMiddleware',

 ]

3)STATICFILES_STORAGE = 
 'whitenoise.storage.CompressedManifestStaticFilesStorage'

4)STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'static'),
)


django_heroku.settings(locals())

#-----------------------urls.py---------------------------------------#
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
 ...,
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

 #------------------Procfile create in Root DIR-----------------#
  paste in  (web: gunicorn projectname.wsgi)

 #--------------create requirements.txt---------------------------#

 pip freeze > requirements.txt

 # runtime.txt  create in Root DIR
 paste in (your python version for ex.python-3.8.5)

 #---------then commands in terminal-------------------------#

 heroku login
 heroku create YOUR_APP_NAME

 ##for Clone the repository.......
 git init
 heroku git:clone -a YOUR_APP_NAME

 ## for Deploy your changes......
 git init
 git add .
 git commit - m "initial"
 git push heroku master

 ## then

 heroku run python manage.py migrate
 heroku run python manage.py createsuperuser