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!
Steps
- Create and activate a virtualenv, install dependancies.
python -m venv <env_name> # Windows
python3 -m venv <env_name> # Other
env\Scripts\activate # Windows
source venv/bin/activate # Other
- Initialize a git repository (
git init
) - Add a
.gitignore
file
Suggested gitignore for Django
- 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
on windows)Create
runtime.py
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
- Add your heroku app to
ALLOWED_HOSTS
:
ALLOWED_HOSTS = ['your_app_name.herokuapp.com', ...]
- Commit your changes:
git add .
git commit -m "Configure ALLOWED_HOSTS"
- Push your changes to heroku branch:
git push heroku master # Or branch name
- Migrate your database:
heroku run python manage.py migrate
- Make sure you have
DEBUG = False
in yoursettings.py
file.
Extras
- Open your app online:
heroku open
- Create admin account:
heroku run python manage.py createsuperuser
- If you have static files, run:
heroku run python manage.py collectstatic
Top comments (0)