Full Stack Django On Google App Engine

7
Full Stack Django on Google App Engine (abbreviated) at Austin PUG February 10, 2010 Percy Wegmann presenting

Transcript of Full Stack Django On Google App Engine

Full Stack Django on Google App Engine(abbreviated)

at Austin PUG February 10, 2010

Percy Wegmann presenting

The Problem

You want to run a recent version of Django on Google App Engine.

Solution 1 – Django app-engine-patch

http://code.google.com/p/app-engine-patch/

Supports authentication frameworkSupports session management and caching backendSupports admin interfaceSupports Django formsSupports a bunch of other things as wellCan be slow because it is almost a complete drop-in replacement for Django (and all of those modules need to get loaded by App Engine)

Solution 2 – Built-in Django 1.1 with App Engine Helper

http://code.google.com/appengine/docs/python/tools/libraries.htmlhttp://code.google.com/p/google-app-engine-django/

Supports authentication frameworkSupports session management and caching backendDoes not support admin interface (App Engine has its own admin)Does not support Django forms (App Engine has its own forms)Faster than app-engine-patch because Django 1.1 is supported by App Engine as a preloaded built-in

How it Works - main.py

import os(os.environ)['DJANGO_SETTINGS_MODULE'] = 'settings'

# Standard Python imports.import sys

from appengine_django import InstallAppengineHelperForDjangoInstallAppengineHelperForDjango("1.1")

from appengine_django import have_django_zipfrom appengine_django import django_zip_path

# Google App Engine imports.from google.appengine.ext.webapp import util

# Import the part of Django that we use here.import django.core.handlers.wsgi

def main(): # Ensure the Django zipfile is in the path if required. if have_django_zip and django_zip_path not in sys.path: sys.path.insert(1, django_zip_path)

# Create a Django application for WSGI. application = django.core.handlers.wsgi.WSGIHandler()

# Run the WSGI CGI handler with that application. util.run_wsgi_app(application)

if __name__ == '__main__': main()

How it Works – static media handling

I put my static media in the folder static in my project root

Then I define static_dir entries in app.yaml

- url: /static/4 static_dir: static/

- url: /static static_dir: static/

- url: /.* script: main.py

And in settings.py I define a MEDIA_ROOT setting that points at this path

MEDIA_URL = “/static/4”

for external permalinks

versioned to allow media changes

dynamic Django urls

How it Works – app.yaml

application: bidtective2version: 1runtime: pythonapi_version: 1

default_expiration: '7d'

handlers:- url: /remote_api script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py secure: optional login: admin

# The below handler is required by bigtable search- url: /bts/.* script: bts/main.py secure: optional login: admin

- url: /admin/.* script: main.py secure: optional login: admin - url: /task/.* script: main.py secure: optional login: admin - url: /static/4 static_dir: static/

- url: /static static_dir: static/

- url: /.* script: main.py