🔖 Day21 - Deployment to Heroku

2018 - 02 - 14
🔖 Day21 - Deployment to Heroku
0. [Introduction] Heroku is a PaaS(Platform As A Service). Although there are other options such as PythonAnyWhere which is Iaas (Infrastructure As A Service), Heroku is more versatile. You can upload/deploy the project by using their command line (CLI) and git via the MacOS terminal, or can even upload by using Dropbox. Since we are not going to simply upload files but deployment, it is better to use CLI. Besides, it is always good to have version control for the project by using git.
1. [SignUp & Heroku Toolbelt] First of all, you need to sign-up at https://heroku.com. Upon registration, you can choose Python as your programming language. Once your account is activated, you can assign a name for your project in the dashboard page. Visit this page https://toolbelt.heroku.com to install their CLI. Once installed, open MacOS terminal, cd to your project folder and type:
$heroku login
Now, you are using your heroku account on the terminal whenever you type 'heroku' commands.
2. [Install packages as adaptors] There are several packages you will need before deployment to Heroku. They are:
  • dj-database-url
  • gunicorn
  • whitenoise
Open terminal, cd to project folder and install them:
$pipenv install dj-database-url gunicorn whitenoise

3. [Create files] Some files are needed to tell Heroku which packages your project is using.
  • Procfile (p.s. Mind the spelling, it's not 'Profile'. No extension is needed.) In Procfile add this:
    web: gunicorn PROJECT_NAME.wsgi --log-file -
  • requirements.txt This one is quit similar to the Pipfile of pipenv, you can copy the paste from the Pipfile.lock and amend it into this format:
  • runtime.txt It tells which version of python is being used:
  • .gitignore This is going to specific which files should be ignored (not uploaded):

4. [Static files] Locally, we should have our static set in settings.py like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
To let Heroku recognize the location of our static files, we need to use the installed package whitenoise. Add this line to settings.py:
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'<--
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
And also the wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bootcamp.settings")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

5. [Database adaptor] Your local database is not going to be used on Heroku. In opposite, you can always backup and download the database from Heroku to local. So treat the online version on Heroku as main project. It is better to use postgresql as databse on Heroku as they support, the package dj-database-url can be used as adaptor. Add the dj-database-url to settings.py and add comment notation to inhibit Django from running local database everytime you push to Heroku, like this:
import dj_database_url
    'default': dj_database_url.config(

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'database_name',
        'USER': 'postgres',
        'DBPASSWORD': config('DBPASSWORD'),
        'HOST': '',
        'PORT': '5432'
When you need to run the project locally (e.g. development), you can move the comment notations to enclose dj-database-url instead.
6. [Create database on Heroku] Before deployment, you need to create the database to your Heroku project: Open browser and head to your dashboard on Heroku, enter 'Resources' tab, search the Add-ons named as Heroku Postgres and add it to your project.
7. [.env settings] Since we are not going to upload the .env as we mentioned in .gitignore, settings inside there needed to be entered to the Heroku dashboard manually. From the dashboard - settings, find the session Reveal Config Vars. If you follow the steps and used python-decouple and dj-database-url, you just need to enter the SECRET_KEY:
8. [Deploy] After all these, your project is now ready to deploy. Copy the Pipfile and Pipfile.lock into your project folder. Also, you should do the git commit before git push, so as to have the current version of your project saved by git. To do so, from MacOS terminal, make sure the working directory is your project folder:
$git init (To initialize a git repo in this folder)
$heroku git:remote -a YOUR_PROJECT_NAME_ON_HEROKU (to connect remote git repo) 
$git status (to see any files changed in your projects)
$git add . (ready all files in your project onto an abstract stage)
$git commit -m 'deploy' (commit files from the abstract stage with a string msg remark)
$git push heroku master (To deploy)
If everything's alright, you should see this line at the end
Verifying deploy... done.


There is no comment yet

New Comment

Please Login to comment