Before You Begin
Purpose
This tutorial shows you how to create a Python application using the Django framework and how to deploy the application to Oracle Application Container Cloud Service.
Time to Complete
30 minutes
Background
Oracle Application Container Cloud Service lets you deploy Java SE, Node.js, PHP, and Python applications to the Oracle Cloud.
What Do You Need?
- Access to an instance of Oracle Application Container Cloud Service
- Python 3.6+
- The package management system pip
- cURL 7.0+ with SSL support
Creating the Application Project
-
Create the
django-basicproject directory in your local system. -
In the
django-basicdirectory, create therequeriments.txtfile and add the following content:django==1.11 djangorestframework==3.6.2 Open a command-line or terminal in Linux and install the project dependencies:
pip install -r requirements.txt -t modulesIn the command-line, set up a new Django project:
django-admin startproject tutorial .Go to the
tutorialdirectory and create an application:python manage.py startapp employeesAdd the
employeeapplication and therest_frameworkapplication toINSTALLED_APPS.Edit thetutorial/settings.pyfile:INSTALLED_APPS = ( ... 'rest_framework', 'snippets.apps.SnippetsConfig', )
Building the Application
Create the
Employeemodel. Go toemployees/models.pyand add the following content:from django.db import models class Employee(models.Model): firstName = models.TextField() lastName = models.TextField() email = models.TextField() phone = models.TextField() title = models.TextField() department = models.TextField() birthdate = models.TextField()In the
employeesdirectory, create a file namedserializer.pyand add the following content:from rest_framework import serializers from employees.models import Employee class EmployeeSerializer(serializers.ModelSerializer): class Meta: model = Employee fields = ('id', 'firstName', 'lastName', 'email', 'phone', 'title', 'department', 'birthdate')Edit the
employees/views.pyfile and add the following:from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from employees.models import Employee from employees.serializers import EmployeeSerializer @csrf_exempt def employee_list(request): """ List all code Employees, or create a new snippet. """ if request.method == 'GET': employees = Employee.objects.all() serializer = EmployeeSerializer(employees, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = EmployeeSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) @csrf_exempt def employee_detail(request, pk): """ Retrieve, update or delete a code employee. """ try: employee = Employee.objects.get(pk=pk) except employee.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = Employee(employee) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = EmployeeSerializer(employee, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': employee.delete() return HttpResponse(status=204)In the
employeesdirectory, create theurls.pyfile and add the following content:from django.conf.urls import url from employees import views urlpatterns = [ url(r'^employees/$', views.employee_list), url(r'^employees/(?P<pk>[0-9]+)/$', views.employee_detail), ]Edit the
tutorial/urls.pyfile to include the employee application's URLs.from django.conf.urls import url, include urlpatterns = [ url(r'^', include('employees.urls')), ]Open the
tutorial/settings.pyfile in a text editor and edit theALLOWED_HOSTSparameter to allow all the hosts:ALLOWED_HOSTS = ['*']Go back the command-line and migrate the database to create the
employeestable.python manage.py makemigration employees python manage.py migrate
Testing Your Application Locally
Open a command-line window (or terminal in Linux) and go to the
django-basicdirectory.Launch the application:
python manage.py runserverOpen other command-line window (or terminal in Linux) and create an employee.
curl -X POST -i -H "Content-Type: application/json" -d "{\"firstName\":\"john\",\"lastName\":\"Gray\",\"email\":\"john.gray@example.com\",\"phone\":\"1203123\",\"birthdate\":\"1975-05-14\",\"title\":\"Developer Manager\",\"department\":\"IT\"}" http://127.0.0.1:8000/employees/Get all the employees.
curl -X GET -i http://127.0.0.1:8000/employees/Update the employee.
curl -X DELETE -i http://127.0.0.1:8000/employees/1/Get the employee by ID.
curl -X GET -i http://127.0.0.1:8000/1/
Preparing Your Application to Cloud Deployment
In the
django-basicdirectory, create thestart.shfile and add the following content:#!/bin/sh export PYTHONPATH=modules python app.pyCreate the
manifest.jsonfile and add the following content:{ "runtime": { "majorVersion": "3.6.0" }, "command": "sh ./start.sh", "isClustered": true, "release": { "version": "1.0" }, "notes": "Python App using Flask and MySQL" }Create the
django-basic-dist.zipfile with the content of thedjango-basicdirectory.zip -r django-basic-dist.zip app.py start.sh manifest.json modules requirements.txt
Deploy Your Application to Oracle Application Container Cloud Service
Open the Oracle Application Container Cloud Service console.
-
In the Applications list view, click Create Application, and select Python.
-
In the Application section, enter a name for your application, select Upload Archive, and click Browse.
-
In the File Upload dialog box, select the
django-basic-dist.zipfile and click Open. -
Keep the default values in the Topology section and click Create.
-
Wait until the application is completely created and copy the URL, you'll use it in the next section.
Your application could take a few minutes to complete the process.

Description of this image
Testing Your Application in the Cloud
- Enter the URL of your application:
Open a command-line window (or terminal in Linux) and create an employee.
curl -X POST -i -H "Content-Type: application/json" -d "{\"firstName\":\"David\",\"lastName\":\"Smith\",\"email\":\"david.smith@example.com\",\"phone\":\"1203123\",\"birthdate\":\"1980-02-02\",\"title\":\"Sales Manager\",\"department\":\"Sales\"}" {{url}}/Get all the employees.
curl -X GET -i {{ url }}/Delete the employee.
curl -X DELETE -i {{ url }}/2/Get the employee by ID.
curl -X GET -i {{ url }}/2/
Want to Learn More?
- Oracle Application Container Cloud Service in the Oracle Help Center
- Django website djangoproject.com