Deploy Django App on Heroku

Sandeep Chauhan
2 min readAug 7, 2020

If you are looking to deploy your Django app for free! Heroku is a free and easy platform to deploy your Django app. Heroku is a good platform especially for the students who want to make their projects live freely. In this article, we will learn to deploy the Django app on Heroku. You have to just follow a few steps.

Create App on Heroku

Go to your Heroku dashboard and create an app there. Set remote to your local git repo or you may also connect repo through Github. Now, add an addon database. I’ll suggest to use “Heroku Postgres”, it is free and easy to configure. If you have done with the above steps and connect your Heroku app to your local working app then you need to configure some settings and need to do little changes.

Create Procfile

Create an empty file named “Procfile” and put your app’s process there. Its format is <process type>: <command> , process type is the name of your command like web, worker and the command is what is going to execute. So, For the Django project, you need to put a single line there in Procfile web: gunicorn myproject.wsgi replace “myproject” by your project name.

Update Requirements

To run our app on Heroku, you need to install some extra packages, so listed them in your projects requirements.txt file

gunicorn==20.0.4
psycopg2==2.8.5
django-heroku==0.3.1
whitenoise==5.1.0
dj-database-url==0.5.0

Database Configurations

So, as suggested we are using Heroku Postgres, then put these lines in the project’s settings file in place of the existing database.

import dj_database_url

DATABASES['default'] = dj_database_url.config(conn_max_age=600)

Static Files Configurations

For static files, you have to check that you have defined STATIC_ROOT and STATIC_URL like os.path.join(BASE_DIR, “staticfiles") and "/static/" respectively. Then you need to configure “whitenoise” middleware. Add a middleware as the first element of middleware list like:

MIDDLEWARE = [
"whitenoise.middleware.WhiteNoiseMiddleware",
.....
]

Other Settings and Push the code

You have almost done, now you need to update ALLOWED_HOSTS = [“*”]. But if you want to set DEBUG=False(which is highly recommended for production) then you need to add proper URL(like ‘.herokuapp.com’) in ALLOWED_HOST in place of “*”.

Now it is the time to push your app code to Heroku. Push your code and boom! you got a URL to access your project. It is live now. You can share it anywhere or with anyone.

For reference, you may also have a look at Heroku's official documentation https://devcenter.heroku.com/categories/working-with-django. If you are still facing any issue then you can comment here or contact me.

Thanks for the read, I hope it helps you or may help you in the future. For being notified about my upcoming great posts, please follow me.

--

--