Heroku is a highly preferred platform to host Simple to Complex Web Applications. It supports a wide array of Frameworks giving you extensive scalability.
Hosting your Web App on Heroku gives you the freedom to perfect your Web App rather than worrying about the platform that it is hosted on.
The below post explores the several steps to follow in hosting your Django Site to Heroku. The list may seem exhaustive at first but will start to become a cake walk once you manage to host your first application.
Applies to Mac: OS X 10 and above
Create a Heroku Account
Visit heroku.com and create an account
Download the Command Line Interface. Using the command line interface gives you incredible features setup your web application using simple scripts.
Open Terminal and from within your Project Directory, login to your heroku account using the below command.
$ heroku login
The browser will open a prompt you to login. Successful login will enable you to continue working from the command line.
Create your Heroku Web App
$ heroku create myfirstwebapp
If available, a domain myfirstwebapp.herokuapp.com will be created instantly. If already used, it will prompt you to enter an alternate name.
$ heroku open
This will open your newly created web app in the browser
Install gunicorn in your Django Project
Green Unicorn (unicorn) is a Web Server Gateway Interface (WSGI) server implementation that will help you package and run your Python Web Application.
$ pip install gunicorn
Compile List of Libraries
You can package the list of libraries pertaining to your specific Django Project. Compiling a list enables these libraries to also be installed on your heroic app at the time of deployment.
$ pip freeze > requirements.txt
The above command will create a requirements.txt file in the root folder of your Project. Please inspect this file.
If your Project is created in a virtual environment, it would only list Libraries pertaining to the specific project. But if it is created within your computers version of Python, it would list all the libraries. You may then have to manually remove unnecessary libraries from requirements.txt file that do not pertain to your Django Project.
Creating a Process File
A Process File (Procfile) is a mechanism for declaring what commands are run by your web applications containers on the platform. In this case, our platform is heroku. The Procfile can be used to declare multiple process types.
Create a filed named Procfile in the root directory of your Django Project.
web: gunicorn projectname.wsgi
- web:, this is the process type which will receive web traffic when deployed
- gunicorn, command needed to run the web process. In short, it allows the python code talk to the web server.
- projectname, replace this your Django Project Name. This is the name of the directory that contains the settings.py file.
Adding the Host
Under settings.py add the hostname of your heroku project under ALLOWED_HOSTS
ALLOWED_HOSTS = ['myfirstwebapp.herokuapp.com']
Git is a distributed open source source control (also referred to as “version control”) system commonly used to track and manage file changes. Git is the preferred version control system for Python projects.
Git will aid in managing the file changes in our Local Django Web App and help to write them to our Heroku Site with ease.
Run the below git commands from the Terminal
$ git init
The above command creates an empty local repository within your project folder that will manage all changes to your project files.
But all the files in our Project folder are not required to be pushed to the Live Deployment. For this we will create a file named .gitignore that will address the list of files to be excluded.
Create a file named .gitignore in your Project root directory and copy the contents from the link provide below. When you review the list of files, you will also notice that we ignore the db.sqlite3 as well. The Live App will use the PostgreSQL instead and hence we do not need the SQLite database.
For Mac machines, add the below line as well to the .gitignore file
Let us now use git to see which files will be added to your Live Deployment (git status).
$ git status $ git add -A $ git commit -m "Initial Project Compilation"
The git add command adds a change in the working directory. It basically tells Git that you want to include updates to a particular files or files in the next commit. Option -A, refers to all files.
The git commit command is used to save your changes to the local repository that we had created initially.
Push to Live Deployment
git push heroku master
If your application does not use any static files (example: Local CSS Files, Images etc), it should successfully get deployed without any errors from the local machine.
Though the deployment was successful without errors, the Web App will return an error as there is no database configured on heroku.
Installing PostgreSQL Database on Mac
Kindly ensure that you follow all the 3 Steps in the Installation procedures including adding the $Path to the environment variable.
Creating PostgreSQL Database on heroku
By default, your heroku app comes with a pre-installed PostgreSQL. Running the below command will list the pre-installed database and other pre-instaleld add ons if any.
If you wish to create your own database, use the below command
$ heroku addons : create heroku-postgresql : hobby-dev
hobby-dev refers to the Free Plan under your heroku account
$ heroku pg
The above command will display details of your PostgreSQL database.
Seamless Deployment and Development Experience
For a seamless deployment and development experience, you will need to install the django-heroku library to your Django Project.
$ pip install django-heroku
If the above command returns an error, kindly ensure that you have your environment variables correctly configured while installing PostgreSQL.
Since a new library has now been installed (django-heroku), you must update the requirements.txt file with the new Library Name and Version
$ pip freeze > requirements.txt
Add the below commands to your settings.py file
$ import django_heroku $ django_heroku.settings(locals())
This will automatically configure Database URL, Allowed Hosts and enable accessibility to your Static Files among other benefits.
Creating Tables on your Live PostgreSQL Database
heroic run python manage.py migrate
Django recommends some important measures before your deploy your Web Application. Make sure to deploy most of them if not all the recommendations listed.
First Release of your Web Application
$ git status $ git add -A $ git commit -m "First Release of Web App" $ git push update master
Your Django Web Application should now be ready to use and share.