[ JagoPG Site ]
This publication is more than a year old. The information can be out-dated.

Deploying Django App on Heroku

I am going to explain the steps that I have followed for deploying an Django 1.8 web application to Heroku.

Required tools:

  • python-2.7
  • django-toolbelt
  • gunicorn
  • whitenoise
  • Account in heroku
  • Heroku toolbelt
  • Git

I am going to suppose that you have a project that runs properly on localhost. Firstly, you have to switch the project from development stage to production stage. For this task, settings.py file has to be modified:

# Disable debugging
DEBUG = False

# Add your heroku application URL
ALLOWED_HOSTS = [ 'localhost', '<HEROKU_APP_NAME>.herokuapp.com' ]

# Set up the database for using PostgreSQL, do not add these lines if you use SQLite
import dj_database_url
DATABASES['default'] = dj_database_url.config()

After installing all tools, go to your project's root folder:

  • Create a file called requirements.txt: in this file you have to write all the dependencies of django. When deploying the project, Heroku will read this file and install all these dependencies on the server. If you created a virtual environment for developing the application, you can only call the command "pip freeze > requirements.txt". On the contrary, you shall add all dependencies by hand following this syntax:

    django==1.8
    geopy==1.11.0
    gunicorn==19.6.0
    whitenoise==3.2
    

    As you can see, each line has a dependency with a version number. You shall add all libraries that your project is using.

  • Create a file called Procfile: this file contains the command that Heroku will read for running the application.

  • All projects have static files, for showing them on the webpage they have to be packaged first. For this task I will use WhiteNoise instead of the Django own tools.

    • Edit settings.py: the following variable is the location that django will search for the static files of the project on the server.

      STATIC_ROOT = 'static/'
      
    • Edit wsgi.py: entry point of the application.

      from django.core.wsgi import get_wsgi_application
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<PROJECTNAME>.settings")
      
      from whitenoise.django import DjangoWhiteNoise
      application = DjangoWhiteNoise(get_wsgi_application())
      
    • For checking whether the files are correctly packaged or not, you can run the following command:

      $ heroku run python manage.py collectstatic --noinput
      
  • Now the files have to be uploaded to Heroku, Git is used for uploading this files. Initialise a new repository if you are not using Git for storing your project (you are lasting to start using it in your own projects).
    $ git init .
    $ git add .
    $ git commit
    
  • It is the time to create a new application if you have not created yet in Heroku - if you have not created it yet, notice that you have to add the application URL to the settings.py file. I am going to login with the heroku terminal application, create a new application - what will store a new remote in your local git - and push the data. When the data is pushed, Heroku will install all dependencies, load static files and run the application.
# login
$ heroku login

$ heroku create <APPLICATION_NAME>
$ heroku git:remote -a <APPLICATION_NAME>

# upload the application
$ git push heroku master

# load database model
$ heroku run python manage.py makemigrations
$ heroku run python manage.py migrate

# deploy the application in a free dyno and open the application in a new browser tab
$ heroku ps:scale web 1
$ heroku open

After opening the website all should be executing as in localhost. If a error response is shown you can check the logs:

$ heroku logs --tail