在GAE中使用django模板

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 cgi
import sys, os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

_DEBUG = True

class 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模板的更详细的说明请参见
[url]http://www.woodpecker.org.cn/obp/django/django-faq/templates.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值