Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 19, 2022 11:02 am GMT

How to host a Django project on Heroku (for free)

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> # Windowspython3 -m venv <env_name> # Otherenv\Scripts\activate # Windowssource 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')
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_herokudjango_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 your settings.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

Original Link: https://dev.to/basicpixel/how-to-host-a-django-project-on-heroku-for-free-1d1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To