Django+Python+GoogleAppEngine+HTML/XHTML+CSS = WEB APP
2010-07-11 16:51
453 查看
(自己根据网上的各种资料总结了一个简洁有效的版本,希望能对这方面的新手有帮助)
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!
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!
相关文章推荐
- 在GAE(Google App Engine)上搭建python2.7的web.py程序
- 这几天买了很多书,最近这2个月的学习目标是:熟悉Linux、C、Python、Web前端基础(HTML、XHTML,CSS)
- Google App Engine Helper for Django 更新
- 学习web开发,理解HTML,XHTML,XML,CSS,JS等技术的关系后,眼界会更宽(pu...
- Google推出Web开发利器:App Engine
- 基于Google App Engine和Django的Hello World
- [转]使用Google App Engine Helper for Django
- Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 区别
- google app engine简明教程---Python版
- Google App Engine ProtoRPC Python API 概述
- Python和Google AppEngine开发基于Google架构的应用软件
- Python中使用django框架进行web开发,模板中无法导入css、js和图片的问题解决
- Web.py Cookbook 简体中文版 - How to use templates on Google App Engine
- 校验身份证号码(Google App Engine Python实现)
- Google App Engine, Python2.7的UnicodeDecodeError bug
- Google App Engine ProtoRPC Python API 概述
- django python html css js
- Google Web Toolkit 和 Google App Engine 综合教程 启蒙篇
- 来自知乎-Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么?
- Google App Engine正式支持Python 2.7