How To Deploy Django Web App on Heroku for Free

Over the last few days, I was looking around the web for a tutorial to deploy a Django web application on Heroku. All I could find was pretty long intimidating blog posts, which could only discourage me from starting the process.

I’ve done some research and finally deployed my app on Heroku today. So, I just want to document the steps that I’ve done on this blog post.

I’m using a Windows machine. If you’re using the same, simply follow these steps. If you’re on a Mac or Linux, I hope these same steps will work. But I haven’t tested it though.

Also, I’m using Anaconda as the default Python. If you’re not using Anaconda, that’s fine. You can open your command prompt instead of opening the Anaconda Prompt, which I’m using.

I’m not showing you how to create a Django web app now. You already know that. That’s why you’re reading this article. I’m deploying the blog app that I created earlier.

This is not a detailed tutorial. I don’t explain to you what the purpose of each step is. I want you to complete the deployment as soon as possible. That’s the goal here. You can worry about the reason behind each step later.

So, here are the steps.

Step 1

You must have a Heroku account. If you don’t have one, why are you wasting your time? Go here and sign up.

You need to have Git installed in your system. If you don’t have, go to this link and install the software.

And the final thing you need is Heroku CLI. Go ahead and install it.

Step 2

Open Anaconda prompt (or your command prompt if you don’t have Anaconda) and go to your project directory using the cd command. Activate your Django environment if you have one.

Login to Heroku by typing in the following:

 heroku login 

Step 3

We need some prerequisites to deploy a web app to Heroku. First of all, we need a procfile.

Add a Procfile in the project root directory by using the following command:

echo web: gunicorn blog.wsgi --log-file - >Procfile

Note that blog is the name of my project. You need to use the name of your project there.

Step 4

Add a runtime.txt file in the project root directory and specify the correct Python version.

So, open your project folder manually and create a txt file called ‘runtime’. Open it and paste the following:

 python-3.6.2 

This is the version of Python that I’m using.

Step 5

Install the required packages in the environment by using the following command.

pip install gunicorn dj-database-url whitenoise psycopg2

Step 6

Add a requirements.txt file using the following command:

pip freeze > requirements.txt

Now, manually go to your project directory and open the requirements.txt file. Remove all other requirements in the file except the following:

dj-database-url==0.4.2
Django==1.11.7
gunicorn==19.7.1
psycopg2==2.7.3.2
pytz==2017.3
whitenoise==3.3.1

Step 7

Let’s set up the static assets and database configuration. Open up settings.py file and make the following changes, preferably at the bottom of the file.

#Static files (CSS, JavaScript, Images)
#https://docs.djangoproject.com/en/2.2/howto/static-files/
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(file)))
STATIC_ROOT  =   os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
#Extra lookup directories for collectstatic to find static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
#Add configuration for static files storage using whitenoise
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
import dj_database_url 
prod_db  =  dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)

Step 8

Add the whitenoise middleware at the top of the middleware list in settings.py file.

 'whitenoise.middleware.WhiteNoiseMiddleware', 

Step 9

Create a Heroku app by using the following command.

heroku create pythonistablogapp

Heroku will let you know if the name is already taken. Give a unique name to your app.

Step 10

You’ll get a domain name. Add your app domain name to ALLOWED_HOSTS in settings.py.

ALLOWED_HOSTS = ['pythonistablogapp.herokuapp.com']

Step 11

Now, open the Git CMD software, and go to your project directory using the cd command. Login to Heroku from GIT CMD using heroku login command.

Once you’re logged in, type in the following commands.

git init
heroku git:remote -a pythonistablogapp
git add .
git commit -m "Initial commit"

I got an error message while doing this step like “*** Please tell me who you are.”…

If you get an error like that, verify your email id and user name using the following commands and do the commit again.

git config --global user.email "youremailid@gmail.com"
git config --global user.name "Your Name"
git commit -m "Initial commit"

Step 12

Push the project to the remote repository using the following command:

git push heroku master

Step 13

Migrate the database.

heroku run python manage.py migrate

Step 14

Open the URL and you’ll see the app running live. If you want to add anything to your app, go to the URL/admin to access your backend database.

That’s it. Class dispersed.

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

7 thoughts on “How To Deploy Django Web App on Heroku for Free

  1. Hi Ashwin,

    Thanks for the article!

    While executing “git push heroku master” command, I am getting the following error:

    ####
    Error while running ‘$ python manage.py collectstatic –noinput’.
    remote: See traceback above for details.
    remote:
    remote: You may need to update application code to resolve this error.
    remote: Or, you can disable collectstatic for this application:
    remote:
    remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
    #####

    And while executing “heroku config:set DISABLE_COLLECTSTATIC=1” command, I am getting the following error:

    ###
    Error: Multiple apps in git remotes
    » Usage: –remote myheroku
    » or: –app nstodoapp
    » Your local git repository has more than 1 app referenced in git remotes.
    » Because of this, we can’t determine which app you want to run this command against.
    » Specify the app you want with –app or –remote.
    » Heroku remotes in repo:
    » nstodoapp (heroku)
    » nstodoapp (myheroku)
    » nstodoapp (staging)
    ###

    Could you please suggest what exactly needs to be done?

  2. How can I deploy mysql databases django project to heroku ..
    And thanks for the tutorial it really helped….

    1. Cheers! Need to take a look at the MySQL deployment. Maybe, I’ll try to create an article for that in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Content