Python Django Buildpack for Cloud Foundry

Image for post
Image for post
Django Buildpack for Cloud Foundry

Django is a powerful python framework to rapidly create apps. This post provides you with a buildpack that will help you accelerate the Django framework installation on cloudfoundry.

This post assumes the following:

  • You have created an app using the cf tools
  • You have created a postgres database service using the cf tools
  • You have bound the app and the postgres database service.

After you create your Django project, using the Terminal on Mac (or) a command prompt on your OS, navigate to the Django folder and then follow the steps listed below:

In the Terminal on Mac (or) the command prompt on your OS, type:

Pip Freeze > requirements.txt

You will see a requirements.txt file in the project folder. This file contains the list of all the dependencies that will be installed on CloudFoundry in support of your Django instance.

In the Procfile enter the following lines. This is to indicate the worker threads that need to be launched to run your Django framework. The buildpack will say hellodjango.wsgi.

web: gunicorn <yourapp>.wsgi — workers 2

The batch file should include the following lines with the sequence. This code will enable you to launch your CloudFoundry Django App via Port 80, make all the migrations, conduct the migrations, create an admin user with admin as the user name and password as the password. You can certainly change these values as you deem fit.

Note: You can add other batch processes like compile messages, make messages into the file.

#!/bin/bashif [ -z “$VCAP_APP_PORT” ];   then SERVER_PORT=80;   else SERVER_PORT=”$VCAP_APP_PORT”;fiecho port is $SERVER_PORTecho “make migrations”python makemigrationsecho “migrate”python migrateecho “from django.contrib.auth.models import User; User.objects.create_superuser(‘admin’, ‘’, ‘password’)” | python shellecho [$0] Starting Django Server…python runserver$SERVER_PORT — noreload

In the manifest.yml, include the following text. You can customize it to your respective app. The most important fields are enclused in “<>”. You can get all the values by using the cf env <appname> command on your command prompt.

— -
- name: APPNAME
memory: 128MB
disk_quota: 256MB
random-route: true
buildpack: python_buildpack
command: bash ./
cf servicename:
label: servicenamelabel
plan: planname
— cf servicename

The buildpack ROOT_URLCONF points to hellodjango. You need to change this to say:

ROOT_URLCONF = ‘app_name.urls’

The wsgi application variable points to the project as well. Change hellodjango to your app name.

WSGI_APPLICATION = ‘app_name.wsgi.application’

In the terminal or the command prompt on your OS, run the cloud foundry command cf env appname. You can extract the database details from JSON and update your accordingly.

After all of the above is setup, use the cf push to setup your App on Cloud Foundry. Fingers crossed, everything should work. The most typical error would be “refused to accept connection” which really means that you have to recheck the values and the identifiers. If everything goes well, you should be able to launch Django and see the classic Django screen.

This buildpack should work on IBM Bluemix and GE Predix.

You can download the CloudFoundry buildpack from this github location. Please note that the requirements.txt is a bit exhaustive. Please run the pip on your project folder or virtualenv to create a more accurate version of the requirements file.

If you are using this template to create your Django Application, please ensure that in your, you change the settings filename to what ever is appropriate for your project.

os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “hellodjango.settings”) //Change hellodjango.settings

GitHub Location For Files

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store