Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

Deploying Your Django App on Heroku

2022-07-25

Heroku is a cloud application platform it's a new way of building and deploying web apps, Which makes easy to host your application in the cloud with a simple git push command.

Heroku supports several programming languages like(Python, Java, PHP)

Install the Heroku Toolbelt:

The first thing you need to do is to install the Heroku toolbelt. In order to interact with heroku service, the toolbelt is best command line software.

Here you can find out the Heroku toolbelt installation for Debian/Ubuntu, Run this from your terminal:

wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

In the below link you need to select your required operating system(Download Heroku Toolbelt for..) for installing heroku toolbelt in your system. And then you can proceed to install.

Please click here to install Heroku ToolBelt

After successfully installation of toolbelt, you can use the heroku command from your terminal.

heroku login
You will prompt to provide heroku credentials(Email and password), once you have authenticated you can access both heroku and git commands.

Create your heroku app:

The following command is used to create an app

heroku create your-app-name

Here 'your-app-name' should be unique, heroku will generate default app-name if you won't specify any app-name.

For creating remote for your app use this command

heroku git:remote -a your-app-name

Define a Procfile:

A Procfile is used to specify the commands need to be executed to start your application on the heroku platform

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.

Here first you need to install gunicorn using pip 

pip install gunicorn

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

Create new file in the root directory of your application as "app.json"

{

    "name": "Your-App-Name",

    "description": "Self-hosted app",

    "repository": "provide your git repository url",

    "scripts": {

    "postdeploy": "python manage.py makemigrations && python manage.py migrate"

  }

}

"app.json" is a json file in which you can define all required variables which are confidential(Ex: environment variables), and other info necessary to run an app on Heroku.

To Load your app Static files you need to use "WhiteNoise", this can be install by using pip 

pip install whitenoise

And also you need to update your "wsgi.py" file too

from whitenoise.django import DjangoWhiteNoise

    application = DjangoWhiteNoise(application)
    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Update STATIC_ROOT in your "settings.py" file for loading your static files 

STATIC_ROOT = (os.path.join(BASE_DIR, "static"))

Database Configuration:

For configuring your database, First you need to install "dj_database_url" using pip

pip install dj_database_url
    pip install psycopg2

    import dj_database_url

    DATABASES = {
        'default': dj_database_url.config(
        default='sqlite:////{0}'.format(os.path.join(BASE_DIR, 'db.sqlite3'))
        )
    }

Dependecies used:

1. dj-database-url
    2. Django
    3. gunicorn
    4. psycopg2
    5. whitenoise

Update all your app dependencies in your "requirements.txt" file which is in the root directory of your app. These are the basic dependencies used.

Deploying Your app on Heroku:

This can be done by using simple git command
        * It will read your Django appication
        * Install your requirements provided
        * Detects your Procfile
        * It will collect your static files.

git push heroku master

After Sucessfully deploying your app, you will be provided with the url to open your app.

you can also run it by using the command.

heroku open