六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 44|回复: 0

在GAE中使用django模板

[复制链接]

升级  86%

49

主题

49

主题

49

主题

秀才

Rank: 2

积分
179
 楼主| 发表于 2013-1-15 03:10:39 | 显示全部楼层 |阅读模式
Google App Engine自带了django框架,开发者可以直接在上面使用django开始web程序,如果你不打算去学django框架,而只是想用它的模板机制,那也是可以的,这里以一个hello, world为例做个演示:

文件:./simple_blog/app.yaml
application: simple-blog
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: simple_blog.py

文件:./simple_blog/simple_blog.py
#!/usr/bin/env python# -*- coding: utf-8 -*-import cgiimport sys, osimport wsgiref.handlersfrom google.appengine.ext import webappfrom google.appengine.ext.webapp import template_DEBUG = Trueclass BaseRequestHandler(webapp.RequestHandler):  """套用模板"""  def render(self, template_name, template_values={}):    values = {      'request': self.request,      'application_name': 'test',    }    values.update(template_values)    directory = os.path.dirname(__file__)    #指定模板文件路径    path = os.path.join(directory, os.path.join('templates', template_name))    self.response.out.write(template.render(path, values, debug=_DEBUG)) class IndexPage(BaseRequestHandler):  def get(self):    self.render('index.html', {      'title': 'Index',      'content': 'Hello, World'    })#配置URL路由application = webapp.WSGIApplication([  ('/', IndexPage)], debug=_DEBUG)def main():  wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__':  main()

文件:./simple_blog/templates/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <head>    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>    <title>{{application_name }} - {{title}}</title>  </head>  <body>    {{content}}  </body></html>

附件是一个完整的google app engine演示例子,关于django模板的更详细的说明请参见
http://www.woodpecker.org.cn/obp/django/django-faq/templates.html
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表