Amazon

2011年8月1日月曜日

Google App Engineでテンプレートを使用する際の注意点

Google App EngineではDjangoを利用することができる。
テンプレートを使う時はテンプレートフォルダを作って、そこにHTMLのひな形ファイルを作成して使用する。

ファイル構成例
tmpl/hello.html
app.yaml
main.py

class TestHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/xhtml+xml; charset=Shift_JIS'
        template_values = {
            'app_id': "test",
            'user_info': "userinfo",
        }
  
        path = os.path.join(os.path.dirname(__file__), 'tmpl/hello.html')
        self.response.out.write(template.render(path, template_values)) 
 


但し、一つ注意するのが、テンプレートファイルは静的ファイル扱いではなくスクリプトファイル扱いとなるので、app.yamlに
- url: /tmpl
  static_dir: tmpl
ではなくて
 - url: /tmpl
  script: tmpl/(.html)
とする。
そうしないとTemplateDoesNotExist: hello.htmlというエラーが出る。

0 件のコメント:

コメントを投稿

Amazon3