Django Heroku Deployment

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. Heroku supports all types of Python applications, including Django applications. Heroku also offer a free plan, which is quite limited, but it is great to get started and to host Django applications. In this article we going to create simple Django application called "djangowebsite" and deploy it to Heroku. You can try with your own project name.

Install Heroku in your system Debian/Ubuntu (For other operating system please refer the reference link given below).

$ wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh

Create a Heroku account and login into the Heroku in your system with your Heroku credentails.

$ heroku login
Enter your Heroku credentials:
Email:

Now we will create a basic Django application.

Create a project directory.

$ mkdir django-heroku-deployment

Change into the project directory.

$ cd django-heroku-deployment

Create a Virtual Environment.

$ virtualenv venv

Activate the Virtual Environment

$ source venv/bin/activate

Intstall Django.

(venv)$ pip install django==1.11.8

Check whether the Django is istalled properly.

(venv)$ pip freeze
Django==1.11.8
pytz==2017.3

Now create a Django project.

(venv)$ django-admin startproject djangowebsite .

The dot (.) in the end of the line is required, in order to create the project files in the root directory itself, instead of creating again a project directory.

Create the database and superuser.

(venv)$ python manage.py makemigrations
(venv)$ python manage.py migrate
(venv)$ python manage.py createsuperuser

Run the sever and make sure the application is working properly.

(venv)$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Django version 1.11.8, using settings 'djangowebsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now we need to install some dependencies.

gunicorn - Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX.
psycopg2 - PostgreSQL database adapter for the Python programming language.
dj-database-url - Parses the DATABASE_URL environment variable and converts it to Django’s desired configuration format, automatically.
whitenoise - serving static files in production.

(venv)$ pip install gunicorn psycopg2 dj-database-url whitenoise

Add static files path in settings.py file.

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

Add the database configuration in settings.py file.

import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

Add Whitenoise configuration to MIDDLEWARE_CLASSES in  settings.py file.

MIDDLEWARE_CLASSES = [
   	...
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

If you’d like gzip functionality enabled, also add the following setting to settings.py file.

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Create a directory static and as for now create a empty file 'static.txt' in it. In order to push static directory to git it should not be empty. In future .js.css and img files will be under static directory.

(venv)$ mkdir static
(venv)$ touch static/static.txt

Heroku web applications require a ProcfileProcfile is used to explicitly declare your application’s process types and entry points. It is located in the root directory.

(venv)$ touch Procfile

Add this line in the Procfile.

web: gunicorn djangowebsite.wsgi --log-file -

Now we need to allow this application to access the HTTP_HOST, so modify the ALLOWED_HOSTS in the settings.py file.

ALLOWED_HOSTS = ['*']

Create .gitignore file to ignore the unwanted files when pushing to git.

(venv)$ touch .gitignore

Add these items to .gitignore file.

venv
*.pyc
db.sqlite3

Now Heroku supports python3 only , not python2. In order to make python2 work we need to define runtime python version, for that create a file called runtime.txt and mention python version.

(venv)$ touch runtime.txt

Add this to runtime.txt file.

python-2.7.14

Now add all the installed packages to the requirements.txt file.

(venv)$ pip freeze > requirements.txt

Finally the folder structure will look like this and it is ready for the deployment.

├── djangowebsite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── manage.py
├── Procfile
├── runtime.txt
├── requirements.txt
└── static
    └── static.txt

Now login in to the Heroku dashbaord and create a new app called 'djangowebsite'. And you will get the Heroku git remote url for the app.

Now initialize the project directoy with git and add the Heroku remote url.

(venv)$ git init
(venv)$ heroku git:remote -a djangowebsite

And check the added remote.

(venv)$ git remote -v
heroku	https://git.heroku.com/djangowebsite.git (fetch)
heroku	https://git.heroku.com/djangowebsite.git (push)

Now push the code to this Heroku remote by using general git commands.

(venv)$ git status
(venv)$ git add --all
(venv)$ git commit -am "Added django website"
(venv)$ git push heroku master

Now you can acceess this application, but the database is not created, to create the database we need to enter into the Heroku console.

(venv)$ heroku run bash --app djangowebsite
Running bash on ⬢ djangowebsite... up, run.2532 (Free)
~ $

It will take you to the Heroku console where your application is pushed, now you can create the database.

$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py createsuperuser

That's it and now we deployed the Django application into Heroku and I hope you enjoyed this article. If you face any issues or problem, please feel free to give your feedbacks in the comment secton. You will find the source code here.

Referecne:
Install Heroku CLI
Heroku Django app configuration

  • Django
  • Deployment
  • Heroku