Django App deployment to Heroku
Deploying my Django app to Heroku on Windows (assuming the Django app is already functioning and a virtual environment is created by pipenv
):
- Register a Heroku account and download/install the Heroku CLI
- Create a Heroku app:
- Navigate to the app folder (where the manage.py file is; all the commands below should be executed from this folder)
- Create a private Git repo on GitHub and follow the instructions on how to push the files to GitHub from this folder:
- Login to Heroku:
heroku login
- Create a new app:
heroku create <heroku_app_name>
- Install packages to pipenv:
- django: if you haven’t already added
- gunicorn: this will run our app on Heroku
- waitress: local serve on Windows
- whitenoise: handle static files
- psycopg2: PostGres database
- And all the other packages used in the app
- Local tests:
- Open up a terminal and activate the venv (I am using the bash terminal in VSCode)
- Run:
waitress-serve --listen=127.0.0.1:8000 <project_name>.wsgi:application
- Make sure it is functioning
- Procfile:
- Create file named
Procfile
and paste web: gunicorn <project_name>.wsgi --log-file -
into it
heroku local
doesn’t work with Windows, but this procfile will be used in Heroku remote
- Create another file named
Procfile_windows
and paste web: py manage.py runserver 127.0.0.1:8000
into it
- Run:
heroku local -f Procfile_windows
- Make sure it is functioning
- We don’t need any requirements.txt file as we already have the
Pipfile
- Additional modifications in settings.py:
- Add
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
at the end of the file
- Add
['127.0.0.1', '<heroku_app_name>.herokuapp.com']
to the ALLOWED_HOSTS
list
- Add
whitenoise.middleware.WhiteNoiseMiddleware
to the MIDDLEWARE
list (2nd element)
- Commit and push the changes
- Connect a GitHub repo to Heroku:
- On Heroku Dashboard, navigate to Deploy and select the GitHub deployment method
- Select the repo and the branch to be used
- Enable automatic deployment
- Do a manual deployment now to first deploy the app
- After the successful deployment, the app should be up and running on Heroku
- The SQLite database is working just fine now but the filesystem is not permanent on Heroku, so it will delete our database soon, we don’t want that so we will use PostGres:
- Modify the database in the settings.py:
'ENGINE': 'django.db.backends.postgresql'
- On the Heroku Dashboard add a new add-on: Heroku PostGres
- On the settings tab you have the
config vars
: reveal config vars
- There is a long string there and we will extract the info from that and att the
NAME
, HOST
, PORT
, USER
, PASSWORD
keys to the DATABASES/default dictionary in the settings.py file
- The string we have is constructed as:
postgres://<USER>:<PASSWORD>@<HOST>:<PORT>/<NAME>
- Migrate the database (this will of course be an empty one):
python manage.py migrate
- Create a new super user:
python manage.py createsuperuser
- Commit and push again
- We can set
DEBUG
to False at the end
The private Git repository is necessary as some of the configurations with the passwords are sitting in the settings.py file.
Alternatively one can push the files directly to Heroku and then there is no need for a GitHub repository. To do this, initialize a git repo in your folder and run heroku git:remote -a <heroku_app_name>
right after the first commit. After that all the steps are the same except you can skip step (9) and just push to Heroku by git push heroku master
.
Links: