GAE や Google Closure Library などの話題を扱います。python 初心者です。

2010年1月14日木曜日

GAE - Django はじめの一歩

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
GAE では Django というテンプレートエンジンが使えるらしい。試してみる。

参考:
Using Templates - Google App Engine - Google Code

ここのサンプルコードをコピペして単純なコードを書く。

main.py
19 import os
 20 from google.appengine.ext import webapp
 21 from google.appengine.ext.webapp import util
 22 from google.appengine.ext.webapp import template
 23
 24 class MainHandler(webapp.RequestHandler):
 25
 26   def get(self):
 27         animals = ['monkey', 'cat', 'dog', 'dolphin']
 28         name = 'happy catsan'
 29         
 30         template_values = {
 31           'animals': animals,
 32           'name': name,
 33         }
 34         
 35         path = os.path.join(os.path.dirname(__file__), 'index.html')
 36         self.response.out.write(template.render(path, template_values))
 37 
 38
 39 def main():
 40   application = webapp.WSGIApplication([('/', MainHandler)],
 41                                        debug=True)
 42   util.run_wsgi_app(application)
 43 
 44 
 45 if __name__ == '__main__':
 46   main()

index.html
1 <html>
  2   <body>
  3     <h1>name={{ name }}</h1>
  4     <ul>
  5     {% for animal in animals %}
  6       <li>{{ animal }}</li>
  7     {% endfor %}
  8     </ul>
  9   </body>
 10 </html>

app.yaml
1 application: closuresample
  2 version: 1
  3 runtime: python
  4 api_version: 1
  5 
  6 handlers:
  7 - url: .*
  8   script: main.py


実行するとこんな感じ。



雰囲気がわかってきた。
#インデントで何度もお叱りを受けてしまったが。。

0 件のコメント:

コメントを投稿