【问题标题】:ModuleNotFound error trying to open Django app in Heroku- issue with gunicorn setup?ModuleNotFound 错误尝试在 Heroku 中打开 Django 应用程序 - gunicorn 设置问题?
【发布时间】:2021-09-10 01:37:08
【问题描述】:

我一直在构建我的第一个 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

当从根目录 (family-django) 运行时,这个新的 gunicorn 命令在本地运行,万岁!那一定是我需要的修复。 但是:在 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

【问题讨论】:

    标签: django heroku gunicorn


    【解决方案1】:
    #----------------------------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
    

    【讨论】:

    • 奇怪!是的,不同之处在于添加了白噪声!我没有意识到 Heroku 需要它来支持静态资产:devcenter.heroku.com/articles/django-assets 最初的错误消息不会让我想到朝那个方向前进,感谢您的推动!
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2020-04-22
    • 2012-09-19
    相关资源
    最近更新 更多