Deploy Django in Heroku

Deploy Python Django app in heroku cloud

PostgreSQL needed since heroku don’t support sqlite3
install pgsql supports(not really required to install in your local but add in requirement.txt)

pip install dj-database-url gunicorn whitenoise psycopg2
pip freeze > requirements.txt

Somehow psycopg2 not installing in my local but I added psycopg2 in requirements.txt

My requirement file looks like this

dj-database-url==0.3.0
Django==1.8
gunicorn==19.3.0
whitenoise==1.0.6
psycopg2

Create file with name just “Procfile” and add

web: gunicorn myappname.wsgi

Create a file “runtime.txt” to have the exact python version my app want

python-2.7.9

Open settings.py and add pgsql db info
myappname/settings.py
add in the end of file

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

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

For static file serving
Open myappname/wsgi.py and add in the end

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

Create git repo if needed

git init
git add .
git commit -m "init"

Login to Heroku and create rapo(heroku tool heve to be installed in local)

heroku Login

heroku create

git push heroku master

heroku ps:scale web=1

Now migrate if needed(since pgsql is not in my local need to update models in heroku)

heroku run python manage.py migrate

Also set super admin since my database is empty

heroku run python manage.py createsuperuser

Open app in browser

heroku open

Posted

in

by

Tags: