(自己根据网上的各种资料总结了一个简洁有效的版本,希望能对这方面的新手有帮助)
In this tutorial I will show you how to get a simple datastore-backed Django application up and running on the Google App Engine. I will assume that you are somewhat familiar with Django and Python.
1, Create a directory where you would like to keep all your Appengine and Django files. Inside of this directory, run the command:
django-admin.py startproject appproject
2, Create a file called main.py at the same level as the folder appproject, and put this code:
import os,sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'appproject.settings'#your own app name
# 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.dispatcher
# 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()
3,Create another file called app.yaml , and put in this code:
application: appproject #use your own app id here
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
4, Navigate to folder appproject, and run the command:
python manage.py startapp
myapp
5, Edit the settings.py so MIDDLEWARE_CLASSES and INSTALLED_APPS look like this.: MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
)
INSTALLED_APPS = (
'appproject.myapp'
)
Also we need to tell Django where to find HTML files which can serve as the basis of displaying the pages. Change TEMPLATE_DIRS to look like this:
import os
ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS =(
ROOT_PATH +'/templates', #DON'T forget to mkdir template
)
6, Django uses regular expressions, to tell which function must handle a given URL. Edit the file urls.py to this:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^$', 'appproject.myapp.views.myviewfunction'),#to be changed here
)
7, You need to define your datamodel in models.py file. (an example here)
from google.appengine.ext import db
class Example(db.Model):
title = db.StringProperty()
created_on = db.DateTimeProperty(auto_now_add = 1)
created_by = db.UserProperty()
def __str__(self):
return '%s' %self.title
def get_absolute_url(self):
return '/youURLS/%s/' % self.key()
8, Edit the views.py file to mapped to the four URL patterns.
from django.http import HttpResponse, HttpResponseRedirect
from appproject.myapp import models
import maybe_something_yours_here
from django.shortcuts import render_to_response
def render(template, payload):
payload['recents'] = models.Poll.all().order('-created_on').fetch(5)
return render_to_response(template, payload)
def index(request):
polls = models.Poll.all().order('-created_on').fetch(20)
payload = dict(polls = polls)
return render('index.html', payload)
9, now we need complete the templates.
10, we can test it now:
dev_appserver.py yourprojectname/
THEN,Point your browser towards
http://127.0.0.1:8080/
11, GREAT! the next, Upload your application to the Google App Engine:
appcfg.py update yourprojectname/
That's all. Enjoy it!