This document introduces how to setting up Django 1.0 framework on Google App Engine (GAE) Application.

GAE includes Django 0.96 in the box already. However, using Django 1.0 brings you the cutting-edge power of Django framework.

The following instructions are showed on windows platform, but it applies to other platforms as well.


  • Python 2.5:
    Download the Python 2.5 package from Python Official Website and install.
    In the following of this document, Python is assumed to be installed in C:\python25, which is the default installation path.

  • Google App Engine SDK:
    Again, Just download the latest SDK package from GAE Google Code Site and install.
    Before installing Google App Engine SDK, Python 2.5 should be installed.

  • Django 1.0:
    Download Django 1.0 source package from Django Official Website, extract it and execute the following command in the extracted directory: install

    The Django source package will be installed under the Python installation directory (ex.

Starting up a Django Project

In the command-line window, switch to the project directory and execute the following commands:

(In the following of this document, the project root directory is assumed to be C:\project_root, and your GAE application name is project)

C:\>cd project_root

C:\project_root>C:\Python25\Scripts\ startproject project

Where project should be replaced by your GAE application name.

After the command is executed, a project directory named project will be created under C:\project_root and the django project files will be generated in the project directory.

Copy Django Code to Project Directory

Copy C:\Python25\Lib\site-packages\django directory to C:\project_root\project.

(After the directory is copied, bin, conf, contrib and other directories should be in C:\project_root\project\django.

Then remove the following directories which is unused in GAE Application:

  • django\bin
  • django\contrib\admin
  • django\contrib\auth
  • django\contrib\databrowse
  • django\test

Add Files to Run Django in the GAE Application

The next step is creating GAE application files which calls the Django application handler.

These files should be placed in C:\project_root\project directory.

  • app.yaml:
    application: [application_name]
    version: 1
    runtime: python
    api_version: 1
    - url: /static
    static_dir: static
    - url: /.*

    Where [application_name] should be replaced by your GAE application name.

    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.
    .path.insert(0, os.path.abspath(os.path.dirname(__file__)))

    # Must set this env var *before* importing any part of Django
    .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):
    .exception('Exception in request:')

    # Log errors.
    , django.core.signals.got_request_exception)

    # Unregister the rollback event handler.

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

    # Run the WSGI CGI handler with that application.

    if __name__ == '__main__':

Updating Django Settings

Modify the file in C:\project_root\project:

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

= (
# 'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',

= (
# 'django.contrib.auth',
# 'django.contrib.sessions',

= os.path.dirname(__file__)
= (
# 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.
+ '/templates',

Testing the Application

Run the GAE local test server by executing the following command in the command-line window:

C:\project_root> project

When the server is up and ready, open http://localhost:8080 in your browser. If you set up everything properly, you should see the Django welcome page.


Now you have a working Django application on Google App Engine! You can write your web application using the power from both Django framework and Google App Engine API.

Additional Documentation



    TBri 發表在 痞客邦 留言(2) 人氣()