2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

Archived | Deploy Django applications to the cloud

Archived content

Archive date: 2020-05-19

This content is no longer being updated or maintained. The content is provided “as is.” Given the rapid evolution of technology, some content, steps, or illustrations may have changed.

Django is an open-source, high-level Python Web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the wrangling of web development, so you can focus on writing your app. If you use Django and IBM Cloud, this tutorial is for you.

Learning objectives

After completing this tutorial, you’ll know how to:

  • Deploy Django applications to IBM Cloud with the help of a Django management script.


To complete this tutorial, you need the following prerequisites:

Estimated time

  • It takes about 30 minutes to complete this tutorial.


Create a Python Cloud Foundry app

Configure your application before deployment

  • Create your requirements.txt file to get ready for deployment. This file lists the applications dependencies.

  • Run pip freeze > requirements.txt to create automatically.

  • Add a Django middleware library by adding dj-static==0.0.6 to the requirements.txt file.

  • Add gunicorn==19.1.1 to the requirements.txt file.

  • Change your wsgi.py file to:

import os

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

application = Cling(get_wsgi_application())
  • myapp is your Django application’s name that contains settings.py file

  • Configure your Project’s static file settings – see the Django docs for additional information

Write a custom django-admin command

  • Add a management/commands directory to one of your Django applications.

  • Django registers a manage.py command for each Python module in that directory that doesn’t have a name beginning with an underscore. For more information, see Writing custom django-admin commands in the Django documentation.

  • Create a python script called bluemix_init.py in the management/commands directory.

  • Paste the following code into the bluemix_init.py script:

from django.conf import settings
from django.core.management.base import BaseCommand
import sys

class Command(BaseCommand):

    args = ''
    help = 'Django deployment script - IBM Cloud'

    def add_arguments(self, parser):

        parser.add_argument('application_name', type=str)

    def handle(self, *args, **options):

        check = "Project successfully configured for: %s"
        application_name = options['application_name']
        base = settings.BASE_DIR
        python_version = sys.version.split(' ')[0]
        project_name = settings.WSGI_APPLICATION.split(".")[0]

            f = open(base + "/Procfile", "w")
            f.write("web: python manage.py migrate && gunicorn {}.wsgi --log-file -".format(project_name))

        except IOError as e:
            check = "Exception at creating Procfile :"
            print ("failed to create Procfile {}".format(e))

            m = open(base + "/manifest.yml", "w")
            m.write(" applications: \n  - name: {} ".format(application_name))

        except IOError as e:
            check = "Exception at creating manifest.yml"
            print ("failed to create manifest.yml - {}".format(e))

            v = open(base + "/runtime.txt", "w")

        except IOError as e:
            check = "Exception at creating runtime.txt"
            print ("failed to create runtime.txt - {}".format(e))

        self.stdout.write(check % str(application_name))

Note: This code works on Python 3.

  • After you create the management script, run the python manage.py bluemix_init {{NAME_OF_YOUR_CLOUD_FOUNDRY_APP}} command.

Note, the {{NAME_OF_YOUR_CLOUD_FOUNDRY_APP}} variable is the name you set for your Python Cloud Foundry app on IBM Cloud (remove the brackets).

  • The script creates a manifest.yml, Procfile and runtime.txt.

  • Check the various python-buildpack versions from the cloudfoundry/python-buildpack documenation. Be sure you are running one of the existing versions or the deployment fails.

  • Changing the python version is also possible. See runtime.txt. (Currently the latest version of python available is 3.6.4.)

Push the application to IBM Cloud

  • From your applications top level directory, run the following commands:
bx api <API-endpoint>
bx login
bx app push
  • After a few minutes of deployment, your application should be up and running.

  • If you have any problems during the deployment process, check your static file configuration or your requirement.txt file.


Deploying Django applications to IBM Cloud depends on manifest.yml, Procfile, and runtime.txt files. A management script makes it easier to prepare the files for deployment.