Introduction

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.

Installation

  • 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:

    setup.py install

    The Django source package will be installed under the Python installation directory (ex.
    C:\Python25\Lib\site-packages\django)

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\django-admin.py 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
    handlers:
    - url: /static
    static_dir: static
    - url: /.*
    script: main.py

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

  • main.py:
    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

Modify the settings.py file in C:\project_root\project:

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',
)

Testing the Application

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


C:\project_root>dev_appserver.py 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.

Conclusions

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

References


創作者介紹
創作者 TBri 的頭像
TBri

Daybreak's Bell

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


留言列表 (2)

發表留言
  • Double9
  • 都看不懂= = ....
  • 阿泰
  • 感謝大大分享這麼棒的資訊,最近想要學習GAE,有機會跟大大請教~~~