GAEをほんのちょっと触ってみた

以下,自分用備忘録.

URLマッピング
main関数で以下のように書くと,http://(アプリケーション名).appspot.com/ にアクセスすると MainHandlerクラスを呼び出し,http://(アプリケーション名).appspot.com/hello にアクセスすると HelloHandlerクラスを呼び出す.

def main():
 application = webapp.WSGIApplication(
  [
   (‘/’, MainHandler),
   (‘/hello’, HelloHandler)
  ],
  debug=True
 )
 wsgiref.handlers.CGIHandler().run(application)

テンプレートの利用
上記の MainHandler は以下の通り.予め viewフォルダにindex.htmlを作成.
ファイルをレンダリングして htmlに返します.最後の行は出力.

class MainHandler(webapp.RequestHandler):
 def get(self):
  fpath = os.path.join(os.path.dirname(__file__),’views’,’index.html’)
  html = template.render(fpath,None)
  self.response.out.write(html)


Comments

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です