Django and Google App Engine Working Together

by Emmanuel D. on October 21, 2010

One of our main concern when a new WEB framework or CMS is its compatibility issues on leading app engine like the Google App. As a web developer and CMS enthusiast, i personally always make sure that the new CMS I am using is compatible with Google App Engine.

Well its a good news for us who are using or planning to use Django framework for it is compatible with Google App Engine.  Both Google App Engine and Django can run the WSGI standard to run applications. With a little modification, you can easily use the Google App Engine Datastore.

Here is a helpful code that i got from Google:

Writing your WSGI Handler in main.py

import logging, os

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

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
# 'project' is the name of the project created with django-admin.py
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

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

def log_exception(*args, **kwds):
    logging.exception('Exception in request:')

# Log errors.
django.dispatch.dispatcher.connect(
    log_exception, django.core.signals.got_request_exception)

# Unregister the rollback event handler.
django.dispatch.dispatcher.disconnect(
    django.db._rollback_on_exception,
    django.core.signals.got_request_exception)

def main():
    # 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()

_________________
if you have other Django and Google App Engine useful resources, Kindly put it in the comments.

Previous post:

Next post: