I've been building my first Django project (just running it locally so far using runserver), and I'm taking the steps to host it on Heroku, adding gunicorn. The build succeeds, but when I try to open the app, the Heroku logs show an exception in worker process:
ModuleNotFoundError: No module named 'mysite.wsgi'
Originally my Procfile had this:
web: gunicorn mysite.wsgi
When I would try that gunicorn command locally, it would work from within the family-django/mysite directory, but when I tried from the root directory (family-django), it would give me the same 'no module named mysite.wsgi' error. According to this post, Heroku's going to try from the root directory, so I updated my Procfile as follows, to tell it to run from the mysite directory instead:
web: gunicorn --chdir mysite mysite.wsgi
This new gunicorn command does work locally when run from the root directory (family-django), hooray!! That must be the fix I need. BUT: after updating the Procfile in Heroku and trying to open the app again, it still fails with that 'no module named mysite.wsgi' error pasted above. So there's some other tweak that Heroku needs for this to run there.
My Django project structure is like this:
family-django
|-mysite
| |-familytree
| |-myauth
| |-mysite
| |-asgi.py
| |-settings.py
| |-urls.py
| |-wsgi.py
|-Procfile
|-requirements.txt
wsgi.py and asgi.py were both created at the start of the project. (Not sure if it's wrong to have both?). wsgi.py has:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
settings.py has the following info for INSTALLED_APPS and 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'
Now that I'd like to get this up on Heroku, they recommend gunicorn, so I've added a requirements.txt:
django
django-heroku
gunicorn
Procfile has:
web: gunicorn --chdir mysite mysite.wsgi
#----------------------------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