Deploying Django on Heroku For development

Ranjan MP
Ranjan MP
Sep 20 · 2 min read

As developers, showcasing our work to the clients require us to host our application on the cloud. Heroku offers a free, yeah a free plan to host our applications.

This tutorial is for devs who want to use Heroku as a development server.

Prerequisites
A Django Project

Step 1 : Installing Dependencies

(myvenv) $ pip install dj-database-url gunicorn whitenoise psycopg2(myvenv) $ pip freeze > requirements.txt

Step 2 : Creating Procfile

Create a file with the filename Procfile in the project root directory, i.e in the directory where the manage.py file exists. The file doesn’t have any extension.

Add the below code to the Procfile file

web: gunicorn mysite.wsgi --log-file -

Replace mysite with your django project name

Step 3 : Creating runtime.txt file

Create a file with the filename runtime.txt in the project root directory, i.e in the directory where the manage.py file exists.

Add the below code to the runtime.txt file

python-3.6.4

Add the python version required for your project. Heroku uses the python version mentioned in this file to run the project.

Step 4 : Configuring settings.py file

In settings.py file add

  • Add allowed hosts
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']
  • Add whitenoise middleware to the Middleware settings
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
]
  • Add Static Root and Static url and staticfiles dir
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
  • Add Static files storage setting
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  • Add these database settings at the last in the settings.py file
import dj_database_url
proj_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(proj_db)

Step 5: Heroku Account (Skip if already done)

Step 6 : Git

If git repository is not initialised in the project root folder. Please initialise a git repo and add all files and do an initial commit.

If git repo is already present, commit the files.

Step 7 : Heroku cli commands

  • Create Heroku app
(myvenv) $ heroku create app_name

Replace app_name with the name of your app

  • Disable Collect Static setting
(myvenv) $ heroku config:set DISABLE_COLLECTSTATIC=1
  • Push the code to Heroku
(myvenv) $ git push heroku master
  • Run Collect Static
(myvenv) $ heroku run python manage.py collectstatic
  • Run Migrations
(myvenv) $ heroku run python manage.py makemigrations(myvenv) $ heroku run python manage.py migrate
  • Create Superuser
(myvenv) $ heroku run python manage.py createsuperuser

Thats it!! Your Django app is hosted on Heroku

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade