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

  1. Create and activate a virtualenv, install dependancies.

  2. Initialize a git repo

  3. Add .gitignore

    Suggested gitignore for Django

  4. Add the following to settings.py:

    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
  5. Create a heroku account, login and install heroku CLI (choco install heroku-cli)

  6. Create runtime.txt and add your project python version. Example:

    python-3.10.1
    
  7. Install gunicorn:

    pip install gunicorn
    
  8. Create Procfile and add the following:

    web: gunicorn myproject.wsgi
    
  9. Install django-on-heroku:

pip install django-on-heroku
  1. Add the following to settings.py:

    # Configure Django App for Heroku.
    import django_on_heroku
    django_on_heroku.settings(locals())
    
  2. Add requirements.txt by running:

    pip freeze > requirements.txt
    
  3. Commit your changes:

    git add .
    git commit -m "Init commit"
    
  4. Login to heroku from the command line:

    heroku login
    
  5. Create a new app:

    heroku create *app_name*
    
  6. Push your changes to heroku branch:

    git push heroku master # Or branch name
    
  7. Migrate your database:

    heroku run python manage.py migrate