Using Django with Google Appengine

Author: Shabda Raaj · Version 1

Contents

About

This is a short tutorial about using Django with Google App Engine. We will show you how to configure a Django application for use with App Engine, and build a simple blog application.

We assume you are familiar with Django and have read through the tutorial chapters.

Work in progress

This setup is “Works on my machine” certified. At the time of writing, deploying a Django app to the production App Engine server was not yet successful.

1. Start a project

django-admin.py startproject GBlog

This creates a Django project managed using manage.py. While you can run it using manage.py runserver, our goal is to run it on the App Engine development server.

2. Download the Appengine SDK

The App Engine SDK is available for Linux, Windows, and Mac. On Linux, make sure dev_appserver.py and appcfg.py are in your PATH.

3. Configure the SDK to use Django's WSGI framework

Create main.py in the same directory as manage.py:

from google.appengine.ext.webapp import util
from django.core.management import setup_environ
import settings

setup_environ(settings)

from django.conf import settings
settings._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher

django.dispatch.dispatcher.disconnect(
    django.db._rollback_on_exception,
    django.core.signals.got_request_exception)

def main():
    application = django.core.handlers.wsgi.WSGIHandler()
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
        

Create app.yaml:

application: blogango
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: main.py
        

4. Building the Blog

App Engine ships with Django 0.96.1. Some APIs differ from newer versions, such as clean_data instead of cleaned_data.

5. Differences between Django and Appengine ORM

6. Defining the datamodel for the blog

class Blog(db.Model):
    title = db.StringProperty(required=True)
    tag_line = db.StringProperty(required=True)
    entries_per_page = db.IntegerProperty(default=10)
        

7. Forms and Views

class CreateEntry(forms.Form):
    title = forms.CharField(max_length=100, required=False)

    def save(self):
        entry = BlogEntry()
        entry.put()
        return entry
        

8. Getting help

If you encounter problems, post a message to the discussion thread. Someone will try to help.

(Scientifically proven fact: Using Django makes people helpful.)