DEV Community

Cover image for Deploy Your First Django App With Heroku
Deepak Raj for Py-Contributors

Posted on

Deploy Your First Django App With Heroku

This article was originally published on CodePerfectPlus.com

Heroku is a cloud platform as a service supporting several programming languages. They support several programming languages like Python, Javascript, Java, Php, Go, Scala and Ruby.
Heroku offer a free plan, it's great to start learning and for the host demo application.

Install Heroku CLI Tool

Create an account on Heroku and then install the Heroku CLI tool from here. It's Command line tool to deploy and manage your Django application.

$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 
Enter fullscreen mode Exit fullscreen mode

Prepare the Django Application

In this tutorial, I will deploy my existing project of GitHub API. It's GitHub API project developed 6 months ago and it's also available on Github and you can clone the repository and try it by yourself.

I will be using Heroku CLI with Git. Your application will be saved in a remote git repository in the Heroku Cloud.

Before starting You Have to add some file to your projects.

  • Add a Procfile in the project root;
  • Add Gunicorn to virtualenv;
  • A runtime.txt to specify the correct Python version in the project root;
  • Configure whitenoise to serve static files.

The Procfile

Create a file named Procfile in the project root with the following content:

web: gunicorn base.wsgi --log-file -
Enter fullscreen mode Exit fullscreen mode

Note: change base with the name of your Django project.

Gunicorn

install Gunicorn in VirtualEnv by command:

$ pipenv install gunicorn
$ pipenv install whitenoise
Enter fullscreen mode Exit fullscreen mode

The runtime.txt

create a filename runtime.txt

python-3.7.6
Enter fullscreen mode Exit fullscreen mode

Set Up The Static Assets

Configure the STATIC-related parameters on settings.py:

Add whienoise to settings.py then in the last of MIDDLEWARE List.

"whitenoise.middleware.WhiteNoiseMiddleware",
Enter fullscreen mode Exit fullscreen mode

Deployment

Almost all configurations are completed.

clone the Repository here if you haven't downloaded from upper link.

git clone https://github.com/codePerfectPlus/Deploy-Django-Applications-on-Heroku
cd Deploy-Django-Applications-on-Heroku
Enter fullscreen mode Exit fullscreen mode

Login to Heroku CLI

$ heroku login
Enter fullscreen mode Exit fullscreen mode

Create a Heroku App

This will be your app name. appname.herokuapp.com.
choose clearly the name for the app.

$ heroku create gitapi-project
Enter fullscreen mode Exit fullscreen mode

Output

Creating โฌข gitapi-project... done
https://gitapi-project.herokuapp.com/https://git.heroku.com/gitapi-project.git
Enter fullscreen mode Exit fullscreen mode

You can simply run heroku create then Heroku will find the app name by itself.

Now login to Heroku Dashboard and access your recently created app:

Click on Deploy

Run the following command for the deployment of Django App.

heroku config:set DEBUG_COLLECTSTATIC=1

$ git add .
$ git commit -am "make it better"
$ git push heroku master
Enter fullscreen mode Exit fullscreen mode

Your Application in Live. Enjoy Your Day, Don't forget to comment.

More Articles by Author

Join for Weekly Updates.

Top comments (1)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good guide nice to see that it is a quite simple process. Going to be deploying Python apps eventually.