Deploy to Heroku


Important steps from the Installation section




Pip installs

Heroku Django Library

pip install django-heroku

Gunicorn - HTTP server

pip install gunicorn

Whitenoise - serve static files directly from the web server

pip install whitenoise



Heroku install CLI and log in with console

Download and install the Heroku CLI

Make sure you are in the same folder as manage.py and log in to Heroku

heroku login
heroku create _your_app_name_



Procfile

Create the Procfile file:

ni Procfile

Inside paste:

web: gunicorn _your_project_name_.wsgi



settings.py

Imports

import django_heroku
import dj_database_url

DEBUG - set to False:

DEBUG = False

Update ALLOWED_HOSTS

ALLOWED_HOSTS = ['heroku_app_name.herokuapp.com', 'my_connected_domain', 'localhost', '127.0.0.1']

MIDDLEWARE: as the second item insert:

Databases section

At the bottom of the file add whitenoise manifest and heroku coinfiguration:




Checks before dsploying

Perform all steps from the Security section

Check the Django official deployment checklist

Run django deploy check

python manage.py check --deploy



Deploy to Heroku

Create the requirements.txt file

pip freeze > requirements.txt

Deploy

git init
heroku git:remote -a nameofherokuapp
git add .
git commit -am "First deployment"
git push heroku master



Connect to PostgreSQL Database

Navigate to the Heroku homepage: APP - Overview - Heroku Postgres - Settings - View Credentials - Copy URI and write it down

settings.py: under the existing DATABASE entry, add:




Run migrations and create the admin

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
git add .
git commit -am "Migrations"
git push heroku master



Collect Static Files

python manage.py collectstatic
git add .
git commit -m 'static'  
git push heroku main



Managing possible errors

Wrong python version (Heroku docs)

python-3.10.1

Unable to publish