Deploying Django project to Heroku platform is very easy, if we install the necessary packages required by Heroku. If you do not have an account with Heroku, you could signup with this platform with free of charge. However, if you signup without credit card, Heroku will allow you to deploy up to 5 web applications with free of charge to its platform. You can deploy your apps written in Node.js, Python, PHP, Ruby, Go, Java, Scala, and Clojure on Heroku. 

 

If you signup with a credit card, you can deploy your web applications up to 100 to Heroku with free of charge. Your credit card is only to make Heroku trust you. In addition, you could still pay for a few apps if you want to have full benefits from Heroku such as your app(s) always running without sleeping after 30 minutes, bigger size of PostgreSQL databases etc.

 

To facilitate the deployment of Django project to Heroku, we can first push our Django project to GitHup and let Heroku copy this project to build and install on its platform. But, this platform requires us to install necessary packages and create necessary files before it can copy, build and install the app on its platform.

 

The first file that Heroku needs is “Procfile” in which we have to write down to tell Heroku what kind of web server we want Heroku use to run our project. The most popular web server to run Django app is Green Unicorn (gunicorn). After creating a Procfile, we should write as below:

 

web: gunicorn multimedia.wsgi

 

Next, we have to install gunicorn package in our Django project to be able to include it in a list of packages to tell Heroku to install them in order to run our Django app.

 

pip install gunicorn

 

Heroku also requires us to create a “runtime.txt” file and write down the version of Python programming language used for our Django app.

 

python-3.8.10

 

The runtime.txt file is very important to tell Heroku the exact version of Python programming language that is compatible with our Django app. If we do not provide this file, Heroku will install the latest version of Python programming language for our app. Currently, the latest version of Python is 3.10.4 that is not compatible with the current version of Django 4.0.5. As the result, if we do not tell Heroku the version of Python, our Django app will not be successfully built and installed on Heroku platform.

 

Next, we must tell Heroku to install “whitenoise” package in order to create a folder under the name “static” to store all static file such as css, JavaScript, images, fonts etc. To do this, we have to write down instructions at the bottom of settings.py file as below:

 

 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

 

In the same way as we did with gunicorn package, we need to install whitenoise package in our virtual environment to be able to include this package in the list of packages to be installed on Heroku platform.

 

pip install whitenoise

 

In addition, we need to add whitenoise middleware in the MIDDLEWARE list in the settings.py file for a static folder and files could be successfully created and be used on Heroku.

 

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

 

Next, we have to import os module from Python standard library into settings.py module for the creation of static folder on Heroku and to define the url to this folder. It also requires us to give the permission to Heroku to host our Django app.

 

import os
...
ALLOWED_HOSTS = ['*']

 

When everything is prepared for Heroku, we have to make a final list to tell Heroku to install all packages for our Django project by writing on Terminal window as below:

 

pip freeze > requirements.txt

 

We will see a file named “requirements.txt” was created with a list of packages to tell Heroku to install them for our Django app.

 

asgiref==3.5.2
backports.zoneinfo==0.2.1
Django==4.0.5
gunicorn==20.1.0
sqlparse==0.4.2
whitenoise==6.1.0

 

We should notice that everything we install in the virtual environment offline are not pushed to GitHub, only the requirements.txt file that is pushed to GitHub.

 

Now, everything is set, what we have to do is to push this Django project to GitHub using Git software. In Linux, Git is preinstalled in the OS, so we do not need to install anything. But, in Windows, this software need to be installed.

 

The commands below is to push our Django project to a repository on GitHub.

 

$ git init
$ git remote add origin https://github.com/Sokhavuth/khmerweb-django.git
$ git add .
$ git commit -m "First commit"
$ git push origin master

 

GitHub: https://github.com/Sokhavuth/khmerweb-django

Heroku: https://khmerweb-django.herokuapp.com