gae Pythonのコードテンプレート

2010/08/01 13:28 に いつかはちょうになる が投稿   [ 2010/08/01 13:35 に更新しました ]
面倒なので以下のコードをテンプレートにしている。ファイルはUTF-8で保存する必要がある。
テンプレートが呼び出すhtmlは適当でok。ログインしていない場合はログイン用のindex.htmlが呼び出され、ログインしている場合はユーザートップページのapp-top.htmlが呼び出される。ソースコードには日本語が入っていても問題ない。


# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import os
import datetime
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if user:
      logoutUrl=users.create_logout_url(self.request.uri)
      
      template_values = {
        'user': user,
        'logoutUrl': logoutUrl,
        }
      path = os.path.join(os.path.dirname(__file__), 'app-top.html')
      self.response.out.write(template.render(path, template_values))
    else:
      loginUrl=users.create_login_url("/")
      template_values = {
        'loginUrl': loginUrl,
        }
      path = os.path.join(os.path.dirname(__file__), 'index.html')
      self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

Comments