This step-by-step beginner tutorial will teach you how to host your local Django project on Heroku for free. I haven't found many easy to follow tutorials on this topic so I decided to make my own after hosting many projects with the mentioned steps. Enjoy!
Create and activate a virtualenv, install dependancies.
Initialize a git repo
Add .gitignore
Add the following to settings.py
:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Create a heroku account, login and install heroku CLI (choco install heroku-cli
)
Create runtime.txt
and add your project python version. Example:
python-3.10.1
Install gunicorn
:
pip install gunicorn
Create Procfile
and add the following:
web: gunicorn myproject.wsgi
Install django-on-heroku
:
pip install django-on-heroku
Add the following to settings.py
:
# Configure Django App for Heroku.
import django_on_heroku
django_on_heroku.settings(locals())
Add requirements.txt
by running:
pip freeze > requirements.txt
Commit your changes:
git add .
git commit -m "Init commit"
Login to heroku from the command line:
heroku login
Create a new app:
heroku create *app_name*
Push your changes to heroku branch:
git push heroku master # Or branch name
Migrate your database:
heroku run python manage.py migrate
DEBUG = False
in your settings.py
file.