Run pip install pipenv
to install pipenv
Run pipenv shell
to create an environment, if does not exist, and activate it.
Run pipenv install python_decouple whitenoise dj_database_url Pillow gunicorn
May take a while.
This should create two files: Pipfile
and Pipfile.lock
. Keep them in the project root.
Sign up on Heroku and install the Heroku Toolbeit
Run heroku login
Run heroku create <app name>
Add a Procfile
file in the project root with the following content
web: gunicorn <project name>.wsgi --log-file -
Run pip freeze >> requirements.txt
In settings.py
, set up as follows:
With PROJECT_ROOT
as
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
Configure STATIC_ROOT
, STATIC_URL
and STATICFILES_STORAGE
as
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
In wsgi.py
, set up as follows:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bootcamp.settings")
application =get_wsgi_application()
Edit your settings.py file and add WhiteNoise to the MIDDLEWARE list. The WhiteNoise middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware:
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
] ```
# Configure Media files
In `settings.py`, add
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') MEDIA_URL = '/media/'
# Database
Add a database to your Heroku app
Run `heroku addons:create heroku-postgresql:hobby-dev`
Update `settings.py` with
import dj_database_url # Place this line preferably at the top from decouple import config # Place this line preferably at the top
SECRET_KEY = config('SECRET_KEY') DEBUG = config('DEBUG', default=False, cast=bool) DATABASES = { 'default': dj_database_url.config( default=config('DATABASE_URL') ) }
Ensure you replace:
a) the initial DATABASES configuration as appropriate, NOT adding
b) the initial DEBUG configuration
c) the initial SECRET_KEY configuration. Copy this somewhere temporarily.
These values tend to be different in the `development` and `production` environments hence their replacement.
Create a `settings.ini` file in the **project root** and add the following (*replace appropriately*):
[settings] DEBUG=True SECRET_KEY= DATABASE_URL=[database type]://[username]:[password]@[host]:[port]/[database name]
Create a `.gitignore` file and add the following line:
settings.ini
We don't need these file in the `production` environment.
# Config vars
Head over to the [Heroku Dashboard](https://id.heroku.com/login) and check into you recently created app.
Go to the `Settings` tab and click `Reveal Config Vars`
Add key `SECRET_KEY` with value as the SECRET_KEY you had copied somewhere.
Add key `DEBUG` with value True. This is for the moment. Ensure you change this once you go live
# Commit file
In your **project root**, run
git add . git commit -m "Initial commit" git push heroku master
# Run migrations
Run `heroku run python manage.py migrate`.
If the app **crashes** because of missing *migrations* (something I experienced), run
python manage.py makemigrations && python manage.py migrate git add migrations/ git commit -m "Add migrations" git push heroku master heroku run python manage.py migrate
# If getting collectstatic error during deploy run these commands:
disable the collectstatic during a deploy
```heroku config:set DISABLE_COLLECTSTATIC=1```
deploy
```git push heroku master```
run migrations (django 1.10 added at least one)
```heroku run python manage.py migrate```
run collectstatic using bower
```heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'```
enable collecstatic for future deploys
```heroku config:unset DISABLE_COLLECTSTATIC```
try it on your own (optional)
```heroku run python manage.py collectstatic```
future deploys should work as normal from now on
You are now good to go!
Thanks to [Simple is Better than Complex](https://simpleisbetterthancomplex.com/tutorial/2016/08/09/how-to-deploy-django-applications-on-heroku.html). One of the very useful sites around!
Thanks to [Heroku Dev Centre](https://devcenter.heroku.com)
Thanks to [Google](https://google.com)
And my awesome efforts ;)