本日リリースのApp Engine SDK 1.4.2から、新しいバージョンのDjangoを利用できるようになっているので、早速使ってみた。
ソースコード
templateをインポートする前に、user_libraryでdjangoのバージョンを指定するだけでよい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/usr/bin/env python # -*- coding: utf-8 -*- import os from google.appengine.ext import webapp from google.appengine.dist import use_library use_library('django', '1.2') from google.appengine.ext.webapp import util, template class MainHandler(webapp.RequestHandler): def get(self): params = { 'message': u'テンプレートエンジンで表示しています。', 'version': str(template.django.VERSION) } print_with_template(self, "index.html", params) def print_with_template(self, view, params = {}): fpath = os.path.join(os.path.dirname(__file__), 'templates', view) html = template.render(fpath, params) self.response.out.write(html) def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main() |
テンプレートファイル。(/template/index.html)
1 2 3 4 5 6 7 8 9 10 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></title> </head> <body> {{message}}<br> バージョン:{{version}} </body> </html> |
実行結果
プレリリース版のリリースノートには1.2.4と記載されていたが、どうやら1.2.5が搭載されているようだ。
1 2 | テンプレートエンジンで表示しています。 バージョン:(1, 2, 5, 'final', 0) |
まとめ
pyファイルの先頭に2行付け加えるだけでDjangoのバージョンを0.96から1.2.5にアップデートできた。
参考記事
Google AppEngine で Django 1.1 を使う