Skip to content

Instantly share code, notes, and snippets.

@mark-mishyn
Last active March 18, 2020 07:56
Show Gist options
  • Save mark-mishyn/3fbdf0ca50ac03b3c321ab28d4385895 to your computer and use it in GitHub Desktop.
Save mark-mishyn/3fbdf0ca50ac03b3c321ab28d4385895 to your computer and use it in GitHub Desktop.
Simple steps to deploy Django project to Heroku (OUTDATED)
1. create new app in dashboard.heroku.com
2. create Procfile
echo "web: gunicorn <app-name>.wsgi --log-file -1" > Procfile
3. add settings file for heroku
from .<base-settings-file> import *
import dj_database_url
ALLOWED_HOSTS = ['<name-app>.herokuapp.com', ]
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
4. install dj-database-url and whitenoise
pip install gunicorn dj-database-url whitenoise
pip freeze | grep -i dj-database-url >> requirements.txt
pip freeze | grep -i whitenoise >> requirements.txt
5. check or configure static:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
6. set env var DJANGO_SETTINGS_MODULE in heroku dashboard (tab "Settings")
7. update wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<PATH_TO_SETTINGS>")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
8. set git remote repository
heroku git:remote -a <app-name>
10. set python version
echo "python-3.6.1" > runtime.txt
11. allow to push empty "static" directory to git
mkdir static && touch static/.keep
9. test app on local server
heroku local web
google-chrome http://0.0.0.0:5000
10. commit changes
git add .
git commit
12. deploy to heroku
git push heroku master
13. migrate DB
heroku run python manage.py migrate
14. create superuser
heroku run python manage.py createsuperuser
15. check deployed application
heroku open
16. configure AWS S3 for media files (optional)
pip install django-storages boto
pip freeze | grep -i django-storages >> requirements.txt
pip freeze | grep -i boto >> requirements.txt
add "storages" to INSTALLED_APPS
add to settings
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_STORAGE_BUCKET_NAME = ''
AWS_SECRET_ACCESS_KEY = ''
AWS_ACCESS_KEY_ID = ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment