ほんとうはtwitterみたいの作ろうとして失敗した時間が無いので、簡易掲示板をつくってみた
(環境:MacOSX10.4, Carbon Emacs, FireFox2)
とりあえずプロジェクト?作成
GoogleAppEngineLauncher.app で New Application。
Application nameを適当につける。ここでは「gaetter」。
app.yamlを書き換える
application: gaetter version: 1 runtime: python api_version: 1 handlers: - url: /static static_dir: static - url: .* script: main.py
main.py
#!/usr/bin/env python # *-# -*- coding: utf-8 -*- import os from google.appengine.ext.webapp import template from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import webapp, db from google.appengine.api import users class Monology(db.Model): user = db.UserProperty() date = db.DateProperty(auto_now_add=True) time = db.TimeProperty(auto_now_add=True) text = db.TextProperty() # image = db.BlobProperty() class MainHandler(webapp.RequestHandler): def get(self): user = users.get_current_user() error = self.request.get("error") query = Monology().all() limit = 50 items = query.order('-time').fetch(limit = limit) template_values = {'user':user, 'items':items} path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, template_values)) class Edit(webapp.RequestHandler): def post(self): user = users.get_current_user() monology = Monology() monology.user = user monology.text = self.request.get('text') monology.put() self.redirect('/') class Login(webapp.RequestHandler): def get(self): self.redirect(users.create_login_url('/')) class Logout(webapp.RequestHandler): def get(self): self.redirect(users.create_logout_url('/')) def main(): application = webapp.WSGIApplication([('/', MainHandler), ('/edit',Edit), ('/login',Login), ('/logout',Logout)], debug=True) run_wsgi_app(application) if __name__ == '__main__': main()
ちなみに新着50個まで表示(limit = 50)
おっとindex.htmlも
忘れるとこだったー
<?xml version="1.0" encoding="utf-8"?> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Gaetter</title> <meta http-equiv="content-language" content="ja" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-style-type" content="text/css" /> <link rel="stylesheet" type="text/css" href="static/css/stylesheet.css" media="all" /> </head> <body> <div id="wrapper"> <ul id="nav"> <a href="/">home</a></li> </ul> <div id="header"> <h1>Entrise</h1> </div> <div id="main"> {% if user %} <p>username: <b>{{ user.nickname }}</b> <a href="/logout"> [Logout]</a> </p> <div class="validation"> <form action="/edit" method="post" accept-charset="utf-8"> <div><textarea name='text' ></textarea></div> <div><input type="submit" value="entry"></div> </form> </div> {% for item in items %} <dt>{{ item.user.nickname }}</dt> <h3>{{ item.text }}</h3> <dd> {{ item.date }}:{{ item.time }} </dd> {% endfor %} {% else %} <dd div class="operation"> <a href="/login">Login</a> </dd> <hr /> {% endif %} </div> </body> </html>
実行してみる
実際にweb上へのっけてみる
Sign in - Google Accountsにログインして、My Applicationsの「Create an Application」で名前登録。(おいらはとりあえずzundapy)
その名前でapp.yamlの「application: 〜」のとこ書き換える。(例 application: zundapy)
んでGAELauncherからDeployして、Webブラウザで確認。