以下は受け取ったリクエストデータをそのままクライアントへ帰すだけのシンプルなものだ。
まずは画面HTMLだ。
index.html
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
</head>
<body>
<form method="get" action="/">
名前:<input type="text" name="name" />
<br/>
性別:<input type="radio" name="sex" value="男性" />男性
<input type="radio" name="sex" value="女性" />女性
<br/>
好きな果物:<select name="fruits" multiple="true">
<option value="りんご">りんご</option>
<option value="みかん">みかん</option>
<option value="いちご">いちご</option>
</select>
<br/>
送信に同意:<input type="checkbox" name="agree" value="はい" /><br/>
<input type="submit" value="送信" />
</form>
<p>結果</p>
名前:<b>{{ name }}</b><br/>
性別:<b>{{ sex }}</b><br/>
好きな果物:
{% for fruit in fruits %}
<b>{{ fruit }}</b>,
{% endfor %}<br/>
同意:<b>{{ agree }}</b>
</body>
</html>
次にお馴染みの helloworld.py だ。
helloworld.py
# -*- coding: utf-8 -*-
import cgi
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import logging
#
# メインハンドラ
#
class MainHandler(webapp.RequestHandler):
def get(self):
# <input name="name" type="text" />
name = self.request.get("name")
logging.info("name:" + name)
# <input name="sex" type="radio" />
sex = self.request.get("sex")
logging.info("sex:" + sex)
# <select name="fruits" multiple>...</select>
fruits = self.request.get("fruits", allow_multiple=True)
for fruit in fruits:
logging.info("fruits:" + fruit)
# <input name="agree" type="checkbox" />
agree = self.request.get("agree", default_value="いいえ")
logging.info("agree:" + agree)
template_values = {
"name" : name,
"sex" : sex,
"fruits" : fruits,
"agree": agree
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
以上である。
ここはひとつポチっとよろしく。
プログラミング Google App Engine
posted with amazlet at 11.06.03
Dan Sanderson
オライリージャパン
売り上げランキング: 40082
オライリージャパン
売り上げランキング: 40082
【GAE for Pythonの最新記事】
- GAE/Pでmemcacheを利用してデ..
- PythonでJST日付をUTC(GMT..
- BeautifulSoupオブジェクトを..
- GAE/Pで詳細なエラーログ(トレース情..
- Pythonでオブジェクトのlistをソ..
- GAE/PでAspyctを使ってAOP(..
- GAE/Pでカスタムタグを作って日付をU..
- GAE/PでBeautifulSoupを..
- GAE/Pでログインが必要なページを取得..
- GAE/Pでファイルアップロード。
- GAE/PでCRONを使ったスケジュール..
- GAE/PでModelをJSON変換する..
- GAE/P向け統合開発環境 Eclips..
- GAE/PとjQueryでJSONデータ..
- GAE+Pythonの標準モジュールだけ..
- GAE+Pythonでテンプレートの共通..
- GAEアプリをアップロードする方法。
- GAE+Pythonでテンプレートエンジ..
- GAE+Pythonでデータストアを操作..
- webappフレームワークを使ったフォー..
