[Beginner] Hosting Django app on Heroku problem

Hello there!

I am new to this forum so forgive me if the category I’ve chosen is incorrect.

I followed a tutorial and built a Django website and now I am trying to deploy it to Heroku.

I am getting the following error:

2022-08-11T00:59:39.443822+00:00 app[web.1]: There was an exception (ModuleNotFoundError) importing your module.
2022-08-11T00:59:39.443834+00:00 app[web.1]: 
2022-08-11T00:59:39.443858+00:00 app[web.1]: It had these arguments: 
2022-08-11T00:59:39.443876+00:00 app[web.1]: 1. No module named 'itpartsecommerce'
2022-08-11T00:59:39.443888+00:00 app[web.1]: 
2022-08-11T00:59:39.626514+00:00 heroku[web.1]: Process exited with status 1
2022-08-11T00:59:39.694383+00:00 heroku[web.1]: State changed from starting to crashed

I tried having my Procfile looking like this:

web: waitress-serve --port=$PORT backend.wsgi:application

And it couldn’t find backend.wsgi, then I thought I must change it to:

web: waitress-serve --port=$PORT backend.backend.wsgi:application

But then it couldn’t find backend.settings.
I am heavily stuck on this and I can’t figure a way out.
Please help me out.

I will not help much as I’m also a beginner, but I did get my app hosted OK on Heroku using gunicorn in the Procfile. Have you tried gunicorn?

Yes I did, but since I am using Windows and GUNICORN is incompatible I couldn’t really keep going, but I read that Waitress is basically doing the same thing so I decided to use that.

I’m using Windows 10, and I successfully deployed my app on Heroku using Gunicorn. I didn’t have to run it, it installed and worked with the push into the cloud.
I also used a postgres database format, I didn’t get my application deployed with the default SQLite database.

Basically, I am also not an expert on this but I have deployed almost 4-5 website flawlessly after doing a lot of research and debugging, and I found a very useful and easy method to deploy django apps on heroku without any hurdles and headaches. So I will explain my workflow (method) of deploying django apps on heroku.

STEP 1: Install required libraries
Install gunicorn library/dependency by using the following command:

pip install gunicorn

Then, install psycopg2 library/dependency by using the following command:

pip install psycopg2

Finally, install django-heroku library/dependency. This is basically the game-changer, and it sets all the required settings for Heroku automatically and you would not need to do anything else rather than creating Procfile and setting gunicorn. SO, install it by using the following command:

pip install django-heroku

STEP 2: Import django-heroku in settings.py
In your settings.py, put this code at the end of the file:

# Configuration for Heroku
import django_heroku

django_heroku.settings(locals())

Don’t forget to save our settings.py after entering this code.

STEP 3: Set up your Procfile and some other required files
Write the following in your Procfile:

web: gunicorn yourprojectname.wsgi --log-file -

Change yourprojectname to the name your project have, and notice the - at the end of the line, don’t forget to write it as well. Save your Procfile.

Create a runtime.txt file and write the version of python in it. For example if you used python’s version 3.10.4 to make your django web, then write it in the runtime.txt in the following syntax (THIS IS IMPORTANT):

python-3.10.4

Finally save your runtime.txt

Place both the Procfile and runtime.txt in the main project folder where you have manage.py.

STEP 4: Deploy to heroku
After all these settings, push your project to heroku using Heroku CLI, you can find the details to use Heroku CLI on their official page and use the following command to log in your heroku account and then

heroku login

then use the following commands to push code to heroku:

git add .
git commit -m "Your comments here"
git push heroku master

After following this method, I hope that you will have successful deployment to heroku.

2 Likes

I want to add that it is absolutely vital you don’t have any unused imports at the top of your scripts before you deploy your application. Remove all unused imports. Some imports may have been generated automatically and shouldn’t be there at all.

“itpartsecommerce” is in your logs. What is this? Is this a library which is important, or unused?

Clean up imports, delete your requirements.txt file and then generate a new one:

pip freeze >> requirements.txt

If you feel ok about resetting your database, do this:

heroku pg:reset

Then you will have to re-create the superuser and remake migrations:

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

This has resolved all issues for me more than once (I’m on Windows btw).

To get my application deployed without static issues/media issues, I have done this in settings:

  # not configuring static files this way

  # STATIC_URL = '/static/'
  # STATICFILES_DIRS = [
  #     BASE_DIR / "static",
  # ]
import os

MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # the path becomes [project dir]\media\
MEDIA_URL = '/media/'

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = "/static/"
django_heroku.settings(locals() )
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'whitenoise.runserver_nostatic',
    'django_filters',
    'django_cleanup.apps.CleanupConfig',
    'dashboard.apps.DashboardConfig', # My app
    
]

Note - I’m using an app called “whitenoise” to serve static files in production, as well as two heroku libraries:

pip install heroku
pip install django_heroku
pip install whitenoise

I have also prevented Heroku from running collectstatic

heroku config:set DISABLE_COLLECTSTATIC=1

More reading here:

This is how I have it changed the database format in settings.py for my Heroku app

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'df32352532kqm1t8',
        'USER': 'ykykmwpsnncxna',
        'PASSWORD': '1319e9ce252523552235b9c272b3',
        'HOST': 'ec2-34-239-241-121.compute-1.amazonaws.com',
        'PORT': '5432',
    }
}

I changed my credentials for this post with some garbage text as I don’t want people to know my credentials.

https://docs.djangoproject.com/en/4.1/ref/settings/#databases

Figure out what SQL resource you have as default. Is it postgres? Well you might need to click into it, click settings, and view your credentials. If you see a postgres format, you’re probably going to need to change your DB format in settings.py.

Log in to your heroku dashboard here:

https://dashboard.heroku.com/apps

Click resources - open your resource - click settings - view credentials.