Published Mar 13, 2017

How to Deploy a Django App on Heroku Easily

How to Deploy a Django App on Heroku Easily

Heroku is a contained-based cloud platform for deploying, managing, and scalling applications. Even though there are other similar platforms, such as OpenShift by Red Hat, Windows Azure, Amazon Web Services, Google App Engine e.t.c, Heroku stands out because it is pretty basic and very easy to work with. It also comes with a free package for small apps.

Deploying a Django app on Heroku can be very tricky. Even with all the tutorials online, it still gets a bit confusing. Sometimes, you run into certain errors that you do not understand. This post is going to teach you easy steps to follow when deploying an app on Heroku.

In this post, I'll assume that you have a good understanding of Django and creating Django apps.

Let's Get Started

Inside your terminal, activate the virtual env for the project and run pip install dj-database-url gunicorn whitenoise. This will install three different packages. First, the dj-database-url will set up the database adapter in the project. We will be running it on Gunicorn in Heroku — that's why we installed gunicorn and whitenoise will serve our static files on Heroku.

Step 1: The first thing you need is a requirements.txt file. This file is important because it tells Heroku the various libraries and packages that need to be installed to run your application. If you do not have this file, you can simply go to your terminal (make sure you are in the virtual env for the project) and run the following command on the root directory of your application: pip freeze >> requirements.txt. This command will put all the libraries currently installed into a new file called requirements.txt.

Step 2: Since Heroku runs on Postgres, we will need to provide an adaptor for it. From your terminal, run pip install psycopg2 or simply add psycopg2==2.6.2 to the requirements.txt file and run pip install.

Step 3: The next thing you will need is a Procfile. This simply tells Heroku what to run. Create a file in your project's root directory and name it Procfile. Open it with a text editor or on your commandline and paste the following into it: web: gunicorn xyz.wsgi. Create a file called xyz.wsgi, you can change the xyz to the name of your app. Make sure it has the .wsgi extension appended to it. This will tell Heroku to run gunicorn with our .wsgi file.

Step 4: In our settings.py file, we need to set DEBUG to False and change ALLOWED_HOSTS to ['*'], since we do not know our Heroku URL yet.

Step 5: In the root directory, create a file and name it runtime.txt. This file will tell Heroku what version of Python to run our app with. Inside the file, paste in python-2.7.10 or whatever version of python you are using.

Step 6: In our settings.py file, replace the DATABASES object with the following code and save:

  import dj_database_url

  DATABASES = {
      'default': dj_database_url.config(
          default='sqlite:////{0}'.format(os.path.join(BASE_DIR, 'db.sqlite3'))
      )
  }

This will allow us to keep using our local SQLite database while using Heroku’s database there.

Step 7: Inside our xyz.wsgi file (or whatever your wsgi.py file is called), add the following line and save:

  from whitenoise.django import DjangoWhiteNoise
  application = DjangoWhiteNoise(application)

Heroku

Step 1: You'll need to install the Heroku toolbelt if you don't already have that. Go to Heroku Toolbelt to install.

Step 2: Create a free Heroku account here.

Step 3: Authenticate your heroku account by running heroku login on your terminal.

Step 4: Run heroku create <your app name> on your terminal. You can just run heroku create and Heroku will give your application a random name.

Step 5: Heroku uses git for its deployments. You can go ahead and run git add . and git commit <commit message> on your terminal.

Step 6: Finally, you can deploy to Heroku by running git push heroku master on your terminal.

Step 7: You can tell Heroku to start this web process by running heroku ps:scale web=1 on your terminal.

Step 8: Since we created a new/empty database on Heroku, we will run migrations. On your terminal, enter heroku run python manage.py migrate and then create a django admin superuser heroku run python manage.py createsuperuser.

You can now visit your app in your browser by running heroku open command on the terminal.

That's it!


If you're interested in learning more about deploying applications to Heroku, here's an article on deploying Crystal app to Heroku and here's how to run Python and Ruby on Heroku!