GAE配置Django App

时间:2013-09-22 17:23:29

标签: python django google-app-engine

有人可以帮助我将django应用程序转换为Google App Engine(GAE)吗?我想知道我的django应用程序中的所有文件并将它们复制到GAE应用程序。但是,我不确定如何配置GAE的默认文件。 main.py文件应该如何运行django应用程序,就像它设计的那样:

main.py

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

的app.yaml

application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: django
  version: "1.3"

builtins:
- django_wsgi: on

1 个答案:

答案 0 :(得分:0)

我在app引擎上运行了一个django应用。我按照这个链接启动并运行。 http://www.allbuttonspressed.com/projects/djangoappengine。与常规django相比,所有配置文件都有很多变化。我现在不使用django因为我喜欢app引擎而且讨厌django。以下是我的一些文件示例。请注意,在你的问题中,你有一个webapp2请求处理程序,你不会使用django这样的东西。它将所有常规视图定义作为函数,而不是像app引擎这样的类。如果您决定尝试这种方法,请告诉我它是怎么回事。

在我按照上面的链接后,这就是我的app.yaml。

application: app
version: production
runtime: python27
api_version: 1
threadsafe: yes

libraries:
- name: django
  version: latest


handlers:
- url: /_ah/queue/deferred
  script: djangoappengine.deferred.handler.application
  login: admin

- url: /_ah/stats/.*
  script: djangoappengine.appstats.application

- url: /.*
  script: djangoappengine.main.application

我的settings.py

# Initialize App Engine and import the default settings (DB backend, etc.).
# If you want to use a different backend you have to remove all occurences
# of "djangoappengine" from this file.
from djangoappengine.settings_base import *
import os

DEBUG = False

TEMPLATE_DEBUG = DEBUG

# Activate django-dbindexer for the default database
DATABASES['native'] = DATABASES['default']
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'}
AUTOLOAD_SITECONF = 'indexes'

INSTALLED_APPS = (

    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.sessions',
    'djangotoolbox',
    'autoload',
    'dbindexer',

    # djangoappengine should come last, so it can override a few manage.py commands
    'djangoappengine',
    )

MIDDLEWARE_CLASSES = (
    # This loads the index definitions, so it has to come first
    'autoload.middleware.AutoloadMiddleware',

    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    )

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'context_processors.general'
    )


ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
    os.path.join(os.path.dirname(__file__), 'media'),

    )

main.py

import os,sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

# 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

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

# Log errors.
#import logging
#def log_exception(*args, **kwds):
#    logging.exception('Exception in request:')
#
#django.dispatch.Signal.connect(
#    django.core.signals.got_request_exception, log_exception)

# Unregister the rollback event handler.
django.dispatch.Signal.disconnect(
    django.core.signals.got_request_exception,
    django.db._rollback_on_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()