Skip to main content

Setting up Django for Heroku Deployment


In this tutorial , we are going to show how to deploy your Django 2.0 web application on the Heroku platform.
This tutorial assumes that you are familiar with the django framework, git and the Linux command line. In addition if you haven't done so already, register an account with Heroku.

Django dependencies SETUP

  1. Install virtual env on your linux machine
$ sudo apt-get install virtual env
  • A virtual environment such as (virtual env) allows you to run your application in an isolated environment from your local machine.
  • This is useful if you have multiple applications on your computer that use different versions of certain software packages( python 2.7 for application 1 and python 3.6.4 for application 2)
2. Navigate to your working directory to Create and Activate the virtual environment
$ virtualenv  .vEnv $ .  .vEnv/bin/activate
  • Take care not to omit the dots!
3. Install django 2.0 using pip( Note: You need to have python3 and pip pre-installed on your local machine)
$ pip install django==2.0
4. In your current working directory, start a new django project called coffee
$ django-admin startproject coffee . $ python3 manage.py runserver
  • The second command will run a local development server on your local machine.Navigate to localhost:8000 on your web browser to see if your project was created successfully.
  • Use ctrl+c to close the server
5. Next, let us install all dependencies required for our django application to run on heroku.
$ pip install gunicorn
$ pip install python-decouple
$ pip install whitenoise
$ pip freeze > requirements.txt
  • Pip freeze allows you to store all your dependencies in one file( i.e. requirements.txt )

HEROKU SETUP

  1. Initialize git and commit all changes we've made so far
$ git add .
$ git commit -m "Initial commit"
2. Login to heroku
$ heroku login
3. Create a heroku application (Note: Your app name must be unique, if endeesa is not available try something else!)
$ heroku create endeesa

4. Add heroku postgres database add-on to your new application
$ heroku addons:create heroku-postgresql:hobby-dev -a endeesa
$ heroku git:remote -a endeesa
5. Your settings.py file should look like this
# Copy your secret key to a new file called .env (See step number 8) import os
from decouple import config
import dj_database_url
..........
SECRET_KEY = config('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = ['*']
.......
DATABASES = { 'default': dj_database_url.config( default=config('DATABASE_URL') )} ........
STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'),) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

6. Your wsgi.py file inside the project folder should look like this:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coffee.settings")
# Remember to change coffe.settings to your_project_name.settings
application = get_wsgi_application()
application = DjangoWhiteNoise(application

7. Create a new file in the root directory and name it Procfile (No extension)
web: gunicorn coffee.wsgi --log-file - # Coffee is the name of your django project - You can change it as required!
8. Create a new file and name it .env (no extension). Add the following contents:
SECRET_KEY='copy-your-secret-key-in-yout-settings .py file before modifying it'
DEBUG=True
9. Create a runtime.txt file and add the following
python-3.6.4
10. Update your requirements.txt file so that it looks like this
dj-database-url==0.5.0 Django==2.0.3
django-postgres-copy==2.3.1
gunicorn==19.7.1
numpy==1.14.2
pandas==0.22.0
psycopg2==2.7.4
python-dateutil==2.7.0
python-decouple==3.1
pytz==2018.3
simplejson==3.13.2
six==1.11.0
whitenoise==3.3.1

DEPLOYING TO HEROKU

  1. After you finish ALL the steps listed above, Deploy your application to heroku as follows
$ heroku config:set DISABLE_COLLECTSTATIC=1
$ git add .
$ git commit -m "Remote site update example"
$ git push heroku master

2. Open the application in your browser by running
$ heroku open $ heroku ps:scale web=1
3. Copy the DATABSE_URL value from heroku app dashboard to your .env file
On your web browser: Go to your application dashboard > settings > Reveal config vars > DATABASE_URL Inside your .env file add: DATABASE_URL: 'pxcyyyyss......'

CONTINUOUS SITE DEVELOPMENT

  1. Now to continue editing your site locally run the following:
$ pip install -r requirements.txt $ heroku local
2. Change ALLOWED_HOSTS=['*'] inside settings.py and check if the application is running on 0.0.0.0:5000 on your web browser 3. Now you can continue developing your django application locally. Remember to disable the DEBUG variable inside settings.py once your site is completed.
Example: $ python manage.py startapp
$ python manage.py makemigrations
4. Update the remote site as follows
$ git add .
$ git commit -m "Remote site update example"
$ git push heroku master

Comments