Deploying Your Django app on Heroku

Reading Time : ~ .

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. The toolbelt is a command line software which makes it easy to interact with the Heroku service through your command line.

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 a mechanism for declaring what commands need to run by your application dynos 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 format file for describing web apps. It declares environment variables, Description, and other information required 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
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to index binary files in django haystack Ramya Ambati

Now we are going to index text content which is stored in structured files such as PDFs, Microsoft Office documents, images, etc using haystack and ...

Continue Reading...
Google plus API integration with django Nikhila Mergu

Using GPlus integration, we can get the user verified email id, general information, friends in a less span of time, and user can also share ...

Continue Reading...
Django Custom Template Tags And Filters Nikhila Mergu

Django Template Tags are simple Python functions that accept a value, an optional argument, and return a value to be displayed on the page.
First, ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties