Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save genomics-geek/c88a12b07fd6edca904515dc4e64e027 to your computer and use it in GitHub Desktop.
Save genomics-geek/c88a12b07fd6edca904515dc4e64e027 to your computer and use it in GitHub Desktop.
Guide on how to migrate your Django web application to be deployed on Heroku

Guide on how to have your Django web application deployed on Heroku

Just use these two guides from Heroku:

If you are migrating an existing Django app, I found these steps to be very helpful!

1. Create the Procfile

The contents should be

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

2. Create runtime.txt

This establishes the Python version to use. The contents should be:

python-3.5.1

3. Update database configuration in base settings

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

Note this assumes you have imported dj_database_url. If not, just add it to your import statements

4. Prepare static assests and file serving

Add the following to the base settings file

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

5. Add whitenoise to wgsi

Adjust the application variable in the wgsi.py file. Make sure the new application variable is below the existing one! Add the following:

from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(application)

6. Create app on Heroku

In the git repo for your app, run:

heroku create

If you are using Webpack - you will need to adjust the Heroku buildpacks to add NodeJS.

heroku buildpacks:add --index 1 heroku/nodejs
heroku buildpacks:add --index 2 heroku/python

7. Now set the environment varialbes in Heroku.

  1. Log in to Heroku
  2. Select your app
  3. Go to settings
  4. Reveal config vars
  5. Add the contents of your .env file

8. Create the staticfiles directory at your project base folder

mkdir staticfiles

Then run and overwrite existing files.

python manage.py collectstatic

9. Build compiled JavaScript build

npm run build-production

After making this change, make sure to commit the build to your code base. Ensure it is the on the branch you will deploy!

10. Push your repo to Heroku

From the command line:

git push heroku master

You should see something similiar to this:

(genomics-geek.com) Michaels-iMac:genomics-geek.com michaelgonzalez$ git push heroku master
Counting objects: 141, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (119/119), done.
Writing objects: 100% (141/141), 273.66 KiB | 0 bytes/s, done.
Total 141 (delta 59), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.5.1
remote:      $ pip install -r requirements.txt
remote:        Collecting dj-database-url==0.4.1 (from -r requirements.txt (line 1))
remote:          Downloading dj-database-url-0.4.1.tar.gz
remote:        Collecting Django==1.9.5 (from -r requirements.txt (line 2))
remote:          Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB)
remote:        Collecting gunicorn==19.4.5 (from -r requirements.txt (line 3))
remote:          Downloading gunicorn-19.4.5-py2.py3-none-any.whl (112kB)
remote:        Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
remote:          Downloading psycopg2-2.6.1.tar.gz (371kB)
remote:        Collecting python-decouple==3.0 (from -r requirements.txt (line 5))
remote:          Downloading python-decouple-3.0.tar.gz
remote:        Collecting requests==2.9.1 (from -r requirements.txt (line 6))
remote:          Downloading requests-2.9.1-py2.py3-none-any.whl (501kB)
remote:        Collecting whitenoise==3.0 (from -r requirements.txt (line 7))
remote:          Downloading whitenoise-3.0-py2.py3-none-any.whl
remote:        Installing collected packages: dj-database-url, Django, gunicorn, psycopg2, python-decouple, requests, whitenoise
remote:          Running setup.py install for dj-database-url: started
remote:            Running setup.py install for dj-database-url: finished with status 'done'
remote:          Running setup.py install for psycopg2: started
remote:            Running setup.py install for psycopg2: finished with status 'done'
remote:          Running setup.py install for python-decouple: started
remote:            Running setup.py install for python-decouple: finished with status 'done'
remote:        Successfully installed Django-1.9.5 dj-database-url-0.4.1 gunicorn-19.4.5 psycopg2-2.6.1 python-decouple-3.0 requests-2.9.1 whitenoise-3.0
remote:
remote:      $ python manage.py collectstatic --noinput
remote:        62 static files copied to '/app/staticfiles', 62 post-processed.
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 57.5M
remote: -----> Launching...
remote:        Released v8
remote:        https://dry-woodland-10065.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/dry-woodland-10065.git
 * [new branch]      master -> master

11. Update DATABASE_URL Environment variable to the Heroku Postgres database

Log in to Heroku and find your project. In the settings section, reveal config vars and copy and paste the Heroku Database URL to DATABASE_URL

12. Set up production database and admin account

heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser

13. Update ALLOWED_HOSTS

Make sure to add the domain of the new Heroku app to your ALLOWED_HOSTS environment variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment