Just use these two guides from Heroku:
- Deploying Python and Django Apps on Heroku
- Migrating an Existing Django Project
- Using Multiple Buildpacks
The contents should be
web: gunicorn config.wsgi --log-file -
This establishes the Python version to use. The contents should be:
python-3.5.1
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
Add the following to the base settings file
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
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)
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
- Log in to Heroku
- Select your app
- Go to settings
- Reveal config vars
- Add the contents of your .env file
mkdir staticfiles
Then run and overwrite existing files.
python manage.py collectstatic
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!
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
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
heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
Make sure to add the domain of the new Heroku app to your ALLOWED_HOSTS environment variable