Django app: from local to Heroku

Vitor Hugo da Silva Lima
3 min readJan 26, 2020

--

This tutorial will have you deploying a simple Django app on Heroku platform in minutes.

Requirements

  • Django
  • Heroku account
  • Heroku cli
  • Python 3.6.7 or up
  • Git

Introduction

This will be a really straight foward tutorial of how create a simple Django app locally and then push the app to heroku. This will be the first post of a series that will cover topics such as CI/CD, storing static files on AWS S3 service, and internacionalization.

Creating simple Django App locally

To create a new Django project, open your terminal, navigate to a folder where your project will be stored, and run the following command to start a new django project:

django-admin startproject medium_tutorial

After that you should validate if your application was succesfully created.

Now navigate inside the medium_tutorial folder and apply the first migrations related to the django admin panel using the following command:

python manage.py migrate

To run your application execute the following command:

python manage.py runserver 0.0.0.0:5000

Go to http://localhost:5000 to see if your application is running. You should see the screen below.

PS: It’s important to know that if you do not specify any ports, your django app will run in the 8000 port as default.

Initialize a git repository in your current project and commit the current files with the following command:

git init
git add .
git commit -m "First commit, django app structure"

Now that we have our application running locally and a git repository initialized, it’s time to deploy our application on Heroku.

Heroku Deployment

Open a terminal e navigate to the project folder and login to heroku using the heroku cli tool using the following command:

heroku login

You will be promped to enter your heroku credentials

Now create a new Heroku app with the following command:

heroku create

When you create an app, a git remote (called heroku) is also created and associated with your local git repository.

To Heroku succesfully deploy a Django app, we need to add the following files in the root folder of our project:

  • runtime.txt (python version of the project)
  • requirements.txt (self explanatory)
  • Procfile ( a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform)
  • Procfile.windows (same as the one above but used to run your application locally on windows machines)

Inside your runtime.txt put:

python-3.6.7

Inside your requirements.txt place the following content:

Django==2.0
psycopg2-binary
gunicorn
django_heroku

Inside your Procfile:

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

Inside your Procfile.windows:

web: python manage.py runserver 0.0.0.0:5000

Now you have to configure heroku to handle your static files. Inside your settings.py file, in the bottom of the file put the following code:

import django_heroku
django_heroku.settings(locals())

Now commit everything and push your code to heroku with the following command:

git push heroku master

Now execute your migrations of your django app on heroku with the following command:

heroku run python manage.py migrate

To view if your deployment was successful open the heroku app with the following command:

heroku open

If you followed every step, you should get the exact page as the one above.

In the next post I will show how to store your static files using the AWS S3 service.

--

--