How to publish Django app on Heroku

  • The Procfile
Create a file named Procfile in the project root with the following content:
web: gunicorn reconstruct.wsgi --log-file -
  • The Requirement File (super important)
The requirements.txt
If you are using a virtualenv and pip you can simply run:
pip freeze > requirements.txt
Django==1.9.8 dj-database-url==0.3.0 dj-static==0.0.6 gunicorn==19.6.0 Unipath==1.0 python-decouple==3 Pillow==3.3.0 Markdown==2.6.6 bleach==1.4.3 psycopg2==2.6.1 whitenoise==3.2
  • The runtime.txt
Create a file named runtime.txt in the project root, and put the specific Python version your project use:
python-3.6.2
  • Set Up The Static Assets
Configure the STATIC-related parameters on settings.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = ‘/static/’
STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + [‘static’]))
  • Install the Whitenoise, and don’t forget to update the requirements.txt:
pip install whitenoise
  • Add the Whitenoise to your Django application in the wsgi.py file:
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
  • Update the settings.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

  • Add config variable to disable static config variable
heroku config:set DISABLE_COLLECTSTATIC=1

Link : https://simpleisbetterthancomplex.com/tutorial/2016/08/09/how-to-deploy-django-applications-on-heroku.html

Link : https://github.com/heroku/heroku-django-template

Setting up Database and Admin and project

  • run the command python manage.py migrate
  • cmd: heroku addons:create heroku-postgresql:hobby-dev — app deploytrials
  • add this code :
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
  • heroku run bash — app deploytrials
  • migrate>> makemigrations>>createsuperuser

Creating New Project Using VS Template

  • Create New Django Project
  • Install virtualenv from VS console
  • python manage.py startapp webapp
  • Include webapp in the project in visual studio
  • Add webapp in installed projects
  • <Database migration >
  • add this in URL file of main project url(r’’,include(‘webapp.urls’)),
  • Create a urls.py in the webapp folder and add the following
  • add the following code in the urls.py in the webapp folder ()
  • add the following code in the views.py
  • weapp └───templates └───webapp and add home.html

Vola

Now porting the same app on heroku

Like what you read? Give Harshit Yadav a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.