Python 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:

#1. Create the requirements.txt

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

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.

#2. Create Procfile

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.

#3. Create run.sh batch file

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 run.sh file.

#4. Create the manifest.yml

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.

#5. Update your Settings.py

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

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

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 settings.py accordingly

#6. Do the cf push

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.

#7. Enjoy Djangoing…

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 manage.py, you change the settings filename to what ever is appropriate for your project.

GitHub Location For Files

2 thoughts on “Python Django Buildpack for Cloud Foundry”

  1. Hey Mani, great post, thank you! Quick question: have you experienced any database flush (data saved disappears after a few days) once the app is pushed to the cloud and records are added to the PostgresDB?

  2. Pingback: Bringing a Python Django app to Cloud Foundry in 2017 | IanHuston.net

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.