Skip to page content Skip to main navigation
English | Site Directory

e.g. "templates" or "datastore"

# Running Django on Google App Engine

Damon Kohler
April 2008

## Introduction

Google App Engine and Django both have the ability to use the WSGI standard to run applications. As a result, it is possible to use nearly the entire Django stack on Google App Engine, including middleware. As a developer, the only necessary adjustment is modifying your Django data models to make use of the Google App Engine Datastore API to interface with the fast, scalable Google App Engine datastore. Since both Django and Google App Engine have a similar concept of models, as a Django developer, you can quickly adjust your application to use our datastore.

## Using Django 0.96.1

Google App Engine includes Django 0.96, so if you are on the latest stable release, there's nothing to install. Just import your Django modules as you would normally. We cover the modifications you will need to include if you are using the Django development version as well.

## Writing your WSGI Handler in `main.py`

To get your Django application launched using the WSGI handler, it's just a few simple steps. Import `util` from `google.appengine.ext.webapp`, and the WSGI handler module from Django. Also, instead of handling exceptions using `manage.py`, we can log all exceptions with Google App Engine, and the logs will be viewable in the Admin console. For more information on logging with Google App Engine, you can read our article here.

    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
    os.environ['DJANGO_SETTINGS_MODULE'] = '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()

Additionally, the `app.yaml` file for your Google App Engine application should look like this:

    application: my_application
    version: 1
    runtime: python
    api_version: 1

    handlers:
    - url: /static
      static_dir: static

    - url: /.*
      script: main.py

You can store the CSS, images, and other static content in your static directory.

## Installing Django Development Version

To use the development version, download the entire Django project, and include it in your application's top level directory. To reduce the number of files you deploy to Google App Engine, you can remove the following directories in your Django folder:

    django/bin
    django/contrib/admin
    django/contrib/auth
    django/contrib/databrowse
    django/test

You will need to modify your main.py to remove the standard version of Django and load the copy you included with your application:

    import logging, os, sys

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

    # Remove the standard version of Django.
    for k in [k for k in sys.modules if k.startswith('django')]:
      del sys.modules[k]

    # Force sys.path to have our own directory first, in case we want to import
    # from it.
    sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

    # Must set this env var *before* importing any part of Django
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

    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()

## Updating Django Settings

Since App Engine does not support Django models, leave all DATABASE_* settings set to an empty string. The authentication and admin middleware and apps should be disabled since they require Django models. The functionality these provide is covered by the App Engine Users API and Admin Console respectively. Sessions also depend on Django models and must be disabled as well. Finally, you need to replace references to 'project.app' with just 'app' and set the path to your template directory dynamically.

    import os
    ROOT_URLCONF = 'urls'  # Replace 'project.urls' with just 'urls'

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
    #    'django.contrib.sessions.middleware.SessionMiddleware',
    #    'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.middleware.doc.XViewMiddleware',
    )

    INSTALLED_APPS = (
    #    'django.contrib.auth',
        'django.contrib.contenttypes',
    #    'django.contrib.sessions',
        'django.contrib.sites',
    )

    ROOT_PATH = os.path.dirname(__file__)
    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or
        # "C:/www/django/templates".  Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        ROOT_PATH + '/templates',
    )

## Using the Development Environment

Google App Engine's SDK includes the web development server `dev_appserver.py` that you can use to deploy your application to the Google App Engine development environment. Also, there is no need to use `manage.py` to sync your database when using the Google App Engine datastore.

## Conclusion

That's it! You can now build your application as you normally would using Django.