What is GAE?
Google App Engine (often referred to as GAE or simply App Engine) is a platform as a service (PaaS) cloud computing platform for developing and hosting web applications in Google-managed data centers. Applications are sandboxed and run across multiple servers.App Engine offers automatic scaling for web applications—as the number of requests increases for an application, App Engine automatically allocates more resources for the web application to handle the additional demand.
We can upload any kind of web applications developed in any programming language like Java, Python, PHP.... etc.,Here in this blog let us see how to upload a simple Django application into the Google App Engine.
Simple procedure to upload a Simple django applicaion to GAE:
STEP 1:First create a simple PyDev Google App Engine Project by following the steps given below.
- Go to File > New > Project
- Choose PyDev Google App Engine Project under PyDev folder.
- Choose your own project name.
- If the Python interpreter for Eclipse has not been configured, you would see the link Please configure an interpreter in the related preferences before proceeding.
- Clicking the link will lead you to the Python Interpreters set up. ClickAuto Config. The system will help add the necessary paths to your PythonPath. Just click OK to proceed.
- Choose the Default interpreter and click Next.
- Browse your installed Google App Engine sdk directory.
- Click OK to include the recommended packages and then click Next.
- Fill in your registered App ID. (given as application identifier while creating the app)
- Choose one of the given starter template and click Finish.
STEP 2:Creating a Django Project folder:
- Create a Django Stand Alone application to project folder of the Django on App Engine application the file manage.py and the folder Project_Name including the inside files __init__.py, settings.py, urls.py.
- Create a new file named main.py in the project folder.
Your application folder should have the simple structure as below:
With this folder structure, we would modify the individual files accordingly as explained below.
- main.py Include the below content in your file. This is to define the WSGI for the web server.
import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
- Remove the settings for DATABASES and WSGI_APPLICATION because we would be using Google App Engine datastore and have defined our WSGI in main.py above.
- Change the setting for ROOT_URLCONF to the correct 'Project_Name.urls'.
ROOT_URLCONF = 'Project_Name.urls'
- Change the setting for DJANGO_SETTINGS_MODULE to the correct "Project_Name.settings".
- Change the runtime to python27 and add threadsafe: true to inform Google App Engine that we are running Python 2.7.
- Change the handlers script to main.application because it is where we define our WSGI.
- Include Django with the corresponding version in the list of libraries because we are going to use it here.
- Set the env_variables - DJANGO_SETTINGS_MODULE to 'Project_Name.settings' so that the application can locate where our Django settings.py is.
application: django-demoapp version: 1 runtime: python27 api_version: 1 threadsafe: true handlers: - url: /.* script: main.application libraries: - name: django version: "1.4" env_variables: DJANGO_SETTINGS_MODULE: 'Project_Name.settings'
STEP 3:Creating your Django application folder:
Creating a Simple Application to show "Hello MicroPyramid"
To create our application, we would proceed with the following steps:
- Create the application folder
- Defining the models(if any)
- Defining the views
- Defining the templates(if any)
- Defining the static file folder(if any)
- Defining the urls
- Create the Application Folder
Create an application folder In the root project folder (together with main.py and manage.py and at the same level with Project_Name folder). A folder with an __init__.py is created as a result.
- Defining the Views
we will define our requierd views here.Here is the simple view that returns a simple Httpresponse to print "Hello MicroPyramid".
from django.http import HttpResponse def home(request): return HttpResponse("hello welcome to google app engine")
- Defining the URLs
We would define a URL to serve the view home. We first modify the urls.py file in the Project_Name package to direct all requests to your application:
from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^', include('firapp.urls')), )
We would then create another PyDev module urls (which is just a fileurls.py) in the firapp package to process all requests related to your application:
from django.conf.urls.defaults import * from firapp.views import home urlpatterns = patterns('', (r'^home/$', home), )
Our application has been completed! The final folder structure would look like below:
STEP 4:To Test Run Your Application
Our application would run on the bundled Google Appp Engine development server.
- Right click on the Project Name
- Select Run As > PyDev: Google App Run
- According to the console, your application can now be accesed at http://localhost:8080 .
STEP 5:To Deploy Your Application
These steps would deploy your application to the real system.
- Right click on the Project Name
- Select PyDev: Google App Engine > Upload
- Input your email and password in the Send to prompt when prompted.
- Your application is now available online at http://your-app-id.appspot.com/