How to deploy Django on Heroku

Make sure requirements.txt is in the root of your project folder.

Add these to your requirements.txt:


Add a Procfile in the root of your project with this content:

web: gunicorn <project_name>.wsgi --log-file -

In case you use a src folder you can CD into it like so:

web: sh -c 'cd src/ && gunicorn <project_name>.wsgi --log-file -'

Add this to your file:

import dj_database_url

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

# Honor the 'X-Forwarded-Proto' header for request.is_secure()

Change your content to this:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Add a runtime.txt file in the project root with this content:


Now initiatie the project like so:

$ heroku create

Then push the first commit like so:

$ git push heroku master

You can open your app like so:

$ heroku open

Viewing the logs live:

$ heroku logs --tail

To rename the auto-generated name to something more clear, do:

 Login to and change the app-name to something with small letters and dashses. e.g. example1-production.

 $ git remote rm heroku
 $ heroku git:remote -a example1-production

Running management commands:

$ heroku run python migrate

Or for more control do, but remember changes made here are not visible in your dyno. Hence, woudn't make sense:

$ heroku run bash

For settings use the environment variables in your

import os

def get_env_variable(var_name):
    """ Get the environment variable or return exception """
        return os.environ[var_name]
    except KeyError:

SECRET_KEY = get_env_variable('SECRET_KEY')
    'dsn': get_env_variable('SENTRY_URL'),

To view the current environment variables:

$ heroku config

To set a config variable:

$ heroku config:set TIMES=2

And to make it actually work:

$ heroku ps:scale web=1