Google App Engineでパラメータの受け渡し
Webアプリケーションで値の受け渡しをする場合、HTMLのフォームからPOSTで渡される場合と、URLの後ろに付けて渡されるGETとがあります。
GETの場合
main.pyのMainHandlerクラスの修正
class MainHandler(webapp.RequestHandler): def get(self): flag = bool(long(self.request.get('param'))) params = {'flag': flag} fpath = os.path.join(os.path.dirname(__file__), 'templates', 'index.html') html = template.render(fpath, params) self.response.out.write(html)
/templates/index.htmlの修正
<body> {% if flag %} <div>Trueです。</div> {% else %} <div>Falseです。</div> {% endif %} </body>
これを、ブラウザで「http://localhost:8080/index?param=1」と「http://localhost:8080/index?param=0」でアクセスしてみてください。
http://hogehoge.com/fugafuga?name1=value&name2=value……
クエリーによるパラメータ送信は、上記の形が一般的です。
この場合、以下のようにして値を取得します。
変数 = self.request.get( パラメータ名 )
POSTの場合
main.pyのMainHandlerクラスの修正
class MainHandler(webapp.RequestHandler): def post(self): str1 = self.request.get('text1'))) params = {'message': u'「' + str1 + '」と入力'} fpath = os.path.join(os.path.dirname(__file__), 'templates', 'index.html') html = template.render(fpath, params) self.response.out.write(html)
/templates/index.htmlの修正
<body> <table> <form method="post" action="/"> <tr> <td><input type="text" name="text1" /></td> <td><input type="submit" /></td> </tr> </form> </table> </body>
普通にページアクセスする場合は、getメソッドが呼び出されますが、POST送信された場合は、postメソッドが呼び出されます。
値の取得は、GETの場合と同じです。