Deploying a Django app to Heroku using Github Repository

Shashank Mohabia
3 min readApr 27, 2020

--

Deploying a Django app on Heroku seems quite a tedious task. In this blog, I tried to list the steps you need to follow to achieve the task using a Github repo. It contains both the project side and Heroku side setup steps.

Project Side Setup:-

Step 1. Install dependencies:-

For Heroku deployment following libraries are required:

  1. dj-database-url
  2. django-heroku
  3. gunicorn
  4. psycopg2
  5. python-decouple
  6. whitenoise

so install them using pip

pip install <library name>==<version>#avoid version if you want the latest

Step 2. Create required files:-

Heroku requires some config files to set up the app and deploy it. We have to create 3 files in the root folder of the project (the folder containing manage.py file):

  1. requirements.txt:

This will store all the dependencies and their versions. To create this run this command:

pip freeze > requirements.txt

This will create a new file or modify the existing requirements file.

2. runtime.txt

This will tell Heroku which python version your project needs. Create a new file with the name “runtime.txt” and add this line.

python-<version>eg. python-3.6.9

3. Procfile

Create a new file with no extension just “Procfile” and add the following line and add your core package(folder which contains WSGI file) name in that:

web: gunicorn <core_package_name>.wsgi --log-file -

Step 3. Update settings.py:-

Few modifications are required in settings.py

  1. Add these imports at the top
import dj_database_url
import django_heroku

2. Set debug to false:

DEBUG = False

3. Add your app’s URL to allowed hosts:

#remove https://from the url
#use * if you want to allow all hosts.
ALLOWED_HOSTS = ['<url>']

4. Add whitenoise middleware:

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]

5. Update default database to use PostgreSQL:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ciba',
}
}

6. Add static root:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

7. Finally, add django_heroku settings at the last of the file:

django_heroku.settings(locals())

Step 4. Push to GitHub:

Now the local setup is done. Update your .gitignore as below:

# Distribution / packaging
.Python
venv/
env/
*.sqlite3
.idea/
# Project asset folders
media/
# Database
db.sqlite3
# Editor directories and files
.idea
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

Initialize a git repository in your project and commit your project to it. Push your code to a GitHub repository (works for both public and private repo). And with that project side setup is completed.

Heroku Side Setup:-

Step 1. Create a Heroku app:-

  1. First of all, create a Heroku account if you don't have one.
  2. Then create an app on Heroku.

Step 2. Add python build pack:

  1. Open your app on Heroku dashboard.
  2. Go to the settings tab and scroll down to build packs.
  3. Create a new build pack and select python from the popped dialogue box.

Step 3. Link your GitHub repository:-

  1. Go to deploy tab and scroll down to Deployment methods.
  2. Select GitHub from the options.
  3. Then search your repo name in the search bar next to your GitHub username there and connect it to Heroku.

Step 4. Deploy:

  1. After linking the GitHub repository, select the branch you want to deploy and deploy it either manually or turn on automatic deployment.

Congratulations you successfully deployed your Django project on Heroku. You can open your app by clicking the button on the top right of the dashboard.

Post Deployment:

Now to interact with your deployed server after deployment install heroku cli. After installation open terminal and type

heroku login 

This will ask you to press any key, do that and a window will get opened in the browser. Log in with your Heroku credentials there and come back to the terminal.

Then to run commands on your app type:

heroku run --app <app_name> shell

This will open a remote shell and then you can run whatever commands you want to run. You can also directly run a command by replacing “shell” with your command.

After the deployment migrate your app to avoid any database errors by opening shell as mentioned above and then running:

python manage.py migrate

You can also create a superuser in the same way.

Feel free to comment if there are doubts or if you feel anything needs to be corrected😀.

Resources:

Creating a RESTful API using Django

Sample repo

--

--