How to Deploy a Django App Using Cloud Foundry

Shedrack Akintayo
Cloud Foundry Foundation
6 min readAug 27, 2020

--

How to Deploy Django Application Using Cloud Foundry
Cloud Foundry + Django (a Python web framework)

In this tutorial, I will demonstrate how to easily deploy a Django application to Cloud Foundry with the power of cf push.

Using any of the available commercial providers is the easiest way for web developers to monitor and deploy an application on Cloud Foundry.

The official certified providers of the Cloud Foundry platform are:

  • Atos Cloud Foundry
  • IBM Cloud Foundry (formerly Bluemix)
  • SAP Cloud Platform
  • SUSE Cloud Application Platform
  • Swisscom Application Cloud
  • VMware Tanzu

Other distributions of Cloud Foundry include:

  • anynines Public PaaS
  • Pivotal Web Services

In my previous tutorial, I demonstrated how we can deploy a Node.js application on Cloud Foundry. Just like my previous tutorial, I’ll be making use of Pivotal Web Services because it’s my favorite commercial distribution of Cloud Foundry and it’s easy to get started with.

Get Started with Cloud Foundry Using a Certified Provider

In order to move forward, you’ll have to create a trial or paid account on any of the commercial providers of your choice. For this tutorial, I’ll be using Pivotal Web Services, but you can decide to follow along with any of the providers of your choice.

After successfully creating an account with any provider of your choice, you should see a dashboard that looks somewhat like this:

Pivotal Web Services Dashboard
Pivotal Web Services Dashboard

This image above is how the dashboard for Pivotal Web Services looks; the dashboard for other providers will look different from this but it should have some similarities.

Prepare Your Application For Deployment

Next, you will deploy a Django (a Python framework) application to Cloud Foundry. The first thing you need to do is to clone the application you need to deploy from GitHub. You can do this by running the following command in your terminal:

git clone https://github.com/hacktivist123/python-django-cloudfoundry-demo

After cloning the repository, you need to move into the GitHub repository that you have created by running the following command in your terminal:

cd python-django-cloudfoundry-demo

Once you have moved into the folder that you created when you cloned the GitHub repository, you have to run the migration for the Django application by running the following command in our terminal:

python manage.py migration

If the migration is successful, you should see the following displayed:

Migration Successful on Terminal window

After running the migration, you need to start the development server by running the following command in your terminal:

python manage.py runserver

If the server starts successfully, you should see the following displayed in your terminal:

python server started on terminal window

If you navigate to http://127.0.0.1:8000/ you should see “Hello World, This is a Django App that has been deployed on Cloud Foundry 😄” displayed in the browser.

Deploy Your Django Application Using Cloud Foundry

Now you just need to deploy your application to Cloud Foundry by running a single command in your terminal, but first you need to log into your Pivotal account via the Cloud Foundry CLI.

Prerequisites

  • Cloud Foundry CLI
  • Any Commercial Distribution Account, in this case Pivotal Web Services
  • Procfile in your application to handle the start command when application is deployed
  • App Requirements file in your application (requirements.txt)

If you have all the requirements above, you can go ahead and deploy the application on Cloud Foundry.

Steps to Deploy the Application

1. Log into your Pivotal Web Services Development Space

The next thing you need to do is to log into your development space (in this case Pivotal Web Services) so you can have direct access to your console right from your terminal. To log in, run the following command:

cf login

After running the above command, you’ll be asked for the API endpoint. Type in:

https://api.run.pivotal.io, and press Enter.

You’ll then be asked for the email address and password you used to create your PWS account. Type that in and you’ll be logged in if all your credentials are correct.

2. Push the Django App to Cloud Foundry

You can easily deploy or push your Django app to Cloud Foundry with just this command:

cf push <app name>

The app name is the preferred name you’d like to give to your application when it deploys, and the app name will also be used by the Cloud Foundry CLI to generate the route to the application on Cloud Foundry.

After running the application, it takes a few seconds for the CLI to finish deploying the application, so sit back and relax.

If the application is deployed successfully, you should see the following message in the terminal:

And the application will also be displayed as running on the Pivotal Web Services dashboard like this:

The above shows the status of your application that was deployed, the name of our application, and also the route to the application that was generated.

If you navigate to the route generated by Cloud Foundry, you’ll see your application live. When I navigate to https://django-hello-world.cfapps.io/ I will see the application displayed live in the browser.

So that’s it! You have successfully deployed a Django application to Cloud Foundry with a single command.

Conclusion

We have successfully deployed a Django application to Cloud Foundry, which was very easy with the power of cf push. You can deploy various types of applications — even static applications — on Cloud Foundry super fast and easy. You can deploy Cloud Foundry on your own infrastructure with the various tools available or you can even deploy on the open source version of Cloud Foundry without using a commercial vendor. Cloud Foundry’s main aim is to provide the best possible developer experience to its users.

The supporting repository for this tutorial can be found here.

Recommended Read and Resources

Cloud Foundry Summit Europe 2020 is built by and for the Cloud Foundry community. Whether you’re new to Cloud Foundry, you’re a long-time contributor building the platform, or you’re using Cloud Foundry to attain your business goals, Cloud Foundry Summit is the place to collaborate with other developers, operators, CIOs and IT professionals to shape the future of the project, share best practices and innovate together.

Dates: Oct 21st & 22nd 2020

The best way to connect with the Cloud Foundry community is to join our Slack Workspace at https://slack.cloudfoundry.org/. Those in the Slack community help you get quickly connected with other members or someone from the Cloud Foundry Foundation.

--

--