DEV Community

Cover image for How To Deploy Django App To Heroku- The Simple Way
Rabbil Yasar Sajal
Rabbil Yasar Sajal

Posted on • Updated on

How To Deploy Django App To Heroku- The Simple Way

Have you ever tried uploading your django app to heroku but felt it was too complicated? If so, in this segment we will look at how you can upload your app to heroku. Don't worry, it is going to be short and to the point.

For this tutorial, I will be assuming you have an app built, so the next step you will be delving into, is on how to deploy your app to heroku.

Keeping that in mind, let's dive in and see how we can setup the app.

Prepare your app

Requirements.txt

If you are already working in a virtualenv you can easily run to create your requirements.txt file.

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

If you want to manually write the version of the package just go to pypi website and find the latest version.
It should look something like this:

asgiref==3.3.4
Django==3.2.3
gunicorn==20.1.0
Pillow==8.2.0
pytz==2021.1
sqlparse==0.4.1
django-heroku==0.3.1
whitenoise==5.2.0
Enter fullscreen mode Exit fullscreen mode

Procfile

  • Create a file with name Procfile (Make sure to not have any extension). The Procfile should be in the same directory as your manage.py file.
  • Install gunicorn.
pip install gunicorn
Enter fullscreen mode Exit fullscreen mode
  • Make sure to add gunicorn to your requirements.txt file.

  • Add the following line to your Procfile. The app name is basically the folder name where you have your wsgi.py file.

web: gunicorn <app_name>.wsgi
Enter fullscreen mode Exit fullscreen mode

settings.py

  • Next we will be installing a package called django-heroku, after installing make sure to add it on the requirements.txt file.
pip install django-heroku
Enter fullscreen mode Exit fullscreen mode
  • Now go to your settings.py and import it on top.
import django_heroku
Enter fullscreen mode Exit fullscreen mode

and paste this at the bottom of the file or else you are going to get a KeyError.

django_heroku.settings(locals())
Enter fullscreen mode Exit fullscreen mode

Django Static

  • Now to setup the static assets.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
Enter fullscreen mode Exit fullscreen mode

This will basically tell django where to look for the static files and which folder to look for when collectstatic is run.

  • Install WhiteNoise and update your requirements.txt file.
pip install whitenoise
Enter fullscreen mode Exit fullscreen mode
  • Next, install WhiteNoise into your Django application. This is done in settings.py‘s middleware section (at the top):
MIDDLEWARE_CLASSES = (
    # Simplified static file serving.
    # https://warehouse.python.org/project/whitenoise/
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
Enter fullscreen mode Exit fullscreen mode
  • Finally, if you would like gzip functionality enabled, also add the following setting to settings.py.
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Enter fullscreen mode Exit fullscreen mode

We will need to create one more file called runtime.txt. This will tell heroku which version of python needs to be installed. This step is optional because heroku will use a python version automatically when building but if you want a specific python version you can add it like this.

python-3.8.5
Enter fullscreen mode Exit fullscreen mode

That is it for configuring our app for deployment. Now we go to the heroku cli and see how to upload the app.

Deployment

If you don't have heroku installed on your machine follow this link

  • login to heroku.
heroku login
Enter fullscreen mode Exit fullscreen mode

After successful login we will be able to create our app directly from the terminal.

  • Create a heroku app
heroku create
Enter fullscreen mode Exit fullscreen mode

This will create a heroku app with a random available name. However, if you want to give a name of your choice, just add the name after heroku create. Make sure the name is unique and available.

If you already have a heroku app and want to add the app as a Git remote, you need to execute

heroku git:remote -a <yourapp>
Enter fullscreen mode Exit fullscreen mode
  • Now add all the files to git and commit.
git add .
git commit -m "deploy heroku"
Enter fullscreen mode Exit fullscreen mode
  • Push all the files and build.
git push heroku master
Enter fullscreen mode Exit fullscreen mode

Once done this will deploy your app. Once deployed, we will need to migrate our database.

heroku run bash
Enter fullscreen mode Exit fullscreen mode

This will give us a quick terminal to control our app. Here you can run all your django commands.

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode
  • This will migrate all our files to the database.

Conclusion

That is all for now. If you have followed all the steps above you should have an app which is now running on heroku. If you have come across any issues please leave a comment or knock-me. I will try to help fix them.

Best of luck. Happy coding :D

Top comments (0)