Heroku is a cloud platform as a service(PaaS) that facilitates the deployment of web applications that supports several programming languages, including Python.
So let's look at how we can deploy a Django application to Heroku easily in no time.
The first thing you need to do is to create an account on heroku. You can create one from here. Once you have created an account you need to install the Heroku Command Line Interface (CLI) that will be used manage and scale your applications, view your application logs, and run your application locally. You also need to have git installed on your machine.
After the Heroku CLI is installed, open your terminal and log in to your heroku account using the following command:
$ heroku login
Now CD into your project folder. We need to add a few files to be able to deploy the app in heroku. The first file you need is a
requirements.txt file. If you have your virtual environment setup and active you can just type the following command on terminal to create your requirements file.
$ pip freeze > requirements.txt
Or you can manually list all dependencies on the
The next thing Heroku web applications require is a
Procfile. Inside your project root folder, create a new file called
Procfile and add the following content:
web: gunicorn myproject.wsgi
You need to change
my-application with the name of your Django project.
Now you need to install something called Gunicorn. If you want to know more about Gunicorn you can follow this link. Use the following command to install Gunicorn:
$ pip install gunicorn
Add gunicorn to your requirements.txt file as well.
In addition you can also add a
runtime.txt file to your app’s root directory that declares the exact version number to use. For example, you can create a new file
runtime.txt and add the following content:
Now you need to make some changes to your
settings.py file. The first thing to do is install
django-heroku. It is a package that automatically configures your Django application to work on Heroku.
$ pip install django-heroku
Be sure to add django-heroku to your requirements.txt file as well.
Now, add the following statement to the top of
Then add the following to the bottom of
# Activate Django-Heroku. django_heroku.settings(locals())
Now we will create a new heroku app. Type the following command on your terminal:
$ heroku create my-app
my-app with your app name. It should be unique and not taken by anyone. Heroku will inform you if the name already exists.
This command will create your heroku app with the name you provided. Now you can visit your heroku dashboard on your browser to see your app. You can also see the default page of your application by using the link in the terminal. The link looks something like this:
We will use git to deploy our project to heroku. CD to your project root folder and initialize the git using following command:
$ git init
You can add
.gitignore file to your project. You can visit .gitignore.io to create
.gitignore file for your project
Now add and commit the files to git:
$ git add --all $ git commit -m 'initial commit'
Before you can push your project, you need to set your remote to heroku.
$ heroku git:remote -a my-app
my-app with your app name.
Now, you can push your project to heroku:
$ git push heroku master
Then, migrate the database:
$ heroku run python manage.py migrate
Congratulations!! The deployment is complete. Your project is now live on heroku.
So, we deployed our project to heroku with not-so-good-looking domain name. Well, we can change that too. Heroku allows you to add a custom domain to your application. You can go through the Heroku Python docs for more information.
Level up every day