Skip to content

Depolying Django

Austin Kong edited this page Feb 9, 2018 · 2 revisions

Create a project with preconfigured settings for deployment on heroku

django-admin startproject --template=https://github.com/heroku/heroku-django-template/archive/master.zip --name=Procfile  mysite

Procfile

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

Create a new heroku app from the command line

heroku create appname

For an existing git repo and existing app, use this command to create an appropriate remote to push to heroku for deployment (if automatic push deployment is not configured)

heroku git:remote -a appname

Deploy to heroku with

git push heroku master

Setup remote database (migrate a local one with loaddata, see above)

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

Run local server Read more

heroku local web

One-off dynos (remote terminal)

heroku run [bash|python]
heroku run python manage.py shell

Check/manage resources

heroku ps
heroku ps:scale web=0
heroku ps:scale web=1

View logs

heroku logs --tail

Open app

heroku open

View releases and rollback

heroku releases
heroku releases:rollback v2

Environment Variables read more

heroku config
heroku config:set DJANGO_DEBUG=''

Generate a new SECRET_KEY

Source

heroku config:set SECRET_KEY=$(python -c 'import random; import string; print("".join([random.SystemRandom().choice("{}{}{}".format(string.ascii_letters, string.digits, string.punctuation)) for i in range(50)]))')

Dependencies

Required dependencies to deploy on heroku:

pip install gunicorn dj-database-url whitenoise psycopg2

django-toolbelt is a meta package requesting the above.

Create dependencies list

pip freeze > requirements.txt

Install dependencies list

pip install -r requirements.txt

If DEBUG=False, remember to run python manage.py collectstatic.

Need to buy EC2 for compute, S3 for storage, and Amazon RDS for PostgreSQL