• 検索結果がありません。

第 9 章 アプリケーションのデプロイ 49

12.3 実習 User Service

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 from google.appengine.api import users

19 import webapp2

20 import logging

21

22 class MainHandler(webapp2.RequestHandler):

23 def get(self):

24 self.response.write('Hello world!')

25 26

27 class MyPageHandler(webapp2.RequestHandler):

28 def get(self):

29 user = users.get_current_user()

30 if user:

31 self.response.headers['Content-Type'] = 'text/plain'

32 self.response.write('Hello, ' + user.nickname())

33 else:

34 self.redirect(users.create_login_url(self.request.uri))

35 36 37

38 def handle_404(request, response, exception):

39 logging.exception(exception)

40 response.write('ページが見つかりませんでした。')

41 response.set_status(404)

42

43 app = webapp2.WSGIApplication([

7012Userサービスを使う

44 ('/', MainHandler),

45 ('/mypage', MyPageHandler)

46 ], debug=True)

47

48 app.error_handlers[404] = handle_404

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 from google.appengine.api import users

19 import webapp2

20 import logging

21

22 class MainHandler(webapp2.RequestHandler):

23 def get(self):

24 self.response.write('Hello world!')

25 26

27 class MyPageHandler(webapp2.RequestHandler):

28 def get(self):

29 user = users.get_current_user()

30 if user:

31 self.response.headers['Content-Type'] = 'text/plain'

32 self.response.write('Hello, ' + user.nickname())

33 else:

34 self.redirect(users.create_login_url(self.request.uri))

35 36 37

38 def handle_404(request, response, exception):

39 logging.exception(exception)

40 response.write('ページが見つかりませんでした。')

41 response.set_status(404)

42

43 app = webapp2.WSGIApplication([

7012Userサービスを使う

44 ('/', MainHandler),

45 ('/mypage', MyPageHandler)

46 ], debug=True)

47

48 app.error_handlers[404] = handle_404

12.3 実習 User Service

HelloWorldアプリケーションに、ログインの処理を追加します。

12.3.1 確認

次のことを確認します。

• ログインしていない場合は、ログイン画面が表示される

• ログインしている場合は、ユーザ名が表示される

12.3.2 解答

解答ドキュメントを参照してください。

12.3. 実習User Service 71

第 13

フォームの作成

13.1 Web ページからデータを送信する

Webページに入力フォームを作成し、データを送信するコードを見てみましょう。

ここでは、TODOリストを作成するサンプルプログラムを使って説明します。

13.1.1 データの送信の仕方

データを送信するには、入力フォームが必要です。ユーザが入力フォームに値を入力し、送信ボタンをクリックす ると、指定したURLにPOSTメソッドでリクエストが投げられます。GAEでは次のような方法で、POSTリク エストの処理を行います。

• まずは、MainHandlerに入力フォームを作成する。入力フォームと送信ボタンが追加されます。ボタンを

クリックすると、同じページにpostされます。

1 class MainHandler(webapp2.RequestHandler):

2 def get(self):

3 self.response.write('<html><body>')

4 self.response.write('<h1>TODOリスト</h1>')

5 self.response.write('<form method="post" action="/">')

6 self.response.write('<input type="text" name="content">')

7 self.response.write('<input type="submit" value="送信" />')

8 self.response.write('</body></html>')

• MainHandlerにPOSTの処理を追加します。

– 入力フォームのname属性の値はcontentなので、contentパラメータがリクエストに追加されます。

プログラムでは、リクエストオブジェクトから、self.request.get(’content’)メソッドを

使ってcontentパラメータの値を取得します。取得した値を、cgi.escape()によって< $などの

第 13

フォームの作成

13.1 Web ページからデータを送信する

Webページに入力フォームを作成し、データを送信するコードを見てみましょう。

ここでは、TODOリストを作成するサンプルプログラムを使って説明します。

13.1.1 データの送信の仕方

データを送信するには、入力フォームが必要です。ユーザが入力フォームに値を入力し、送信ボタンをクリックす ると、指定したURLにPOSTメソッドでリクエストが投げられます。GAEでは次のような方法で、POSTリク エストの処理を行います。

• まずは、MainHandlerに入力フォームを作成する。入力フォームと送信ボタンが追加されます。ボタンを

クリックすると、同じページにpostされます。

1 class MainHandler(webapp2.RequestHandler):

2 def get(self):

3 self.response.write('<html><body>')

4 self.response.write('<h1>TODOリスト</h1>')

5 self.response.write('<form method="post" action="/">')

6 self.response.write('<input type="text" name="content">')

7 self.response.write('<input type="submit" value="送信" />')

8 self.response.write('</body></html>')

• MainHandlerにPOSTの処理を追加します。

– 入力フォームのname属性の値はcontentなので、contentパラメータがリクエストに追加されます。

プログラムでは、リクエストオブジェクトから、self.request.get(’content’)メソッドを

使ってcontentパラメータの値を取得します。取得した値を、cgi.escape()によって< $などの

特殊記号をHTMLで正しく表示できる文字列に変換します。

1 def post(self):

2 content = cgi.escape(self.request.get('content'))

3 self.response.write('New Todo:' + content)

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 import cgi

19 import os

20 from google.appengine.api import users

21 import jinja2

22 import webapp2

23 import logging

24

25 JINJA_ENVIRONMENT = jinja2.Environment(

26 loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),

27 extensions=['jinja2.ext.autoescape'],

28 autoescape=True)

29 30

31 class MainHandler(webapp2.RequestHandler):

32 def get(self):

33 self.response.write('<html><body>')

34 self.response.write('<h1>TODOリスト</h1>')

35 self.response.write('<form method="post" action="/">')

36 self.response.write('<input type="text" name="task">')

7413章 フォームの作成

37 self.response.write('<input type="submit" value="送信" />')

38 self.response.write('</form>')

39 self.response.write('</body></html>')

40

41 def post(self):

42 task = cgi.escape(self.request.get('task'))

43 self.response.write('New Todo:' + task)

44 45 46

47 class MyPageHandler(webapp2.RequestHandler):

48 def get(self):

49 user = users.get_current_user()

50 if user:

51 self.response.headers['Content-Type'] = 'text/plain'

52 self.response.write('Hello, ' + user.nickname())

53 else:

54 self.redirect(users.create_login_url(self.request.uri))

55 56

57 def handle_404(request, response, exception):

58 logging.exception(exception)

59 response.write('ページが見つかりませんでした。')

60 response.set_status(404)

61 62

63 app = webapp2.WSGIApplication([

64 ('/', MainHandler),

65 ('/mypage', MyPageHandler)

66 ], debug=True)

67

68 app.error_handlers[404] = handle_404

確認

http://localhost:8080にアクセスするとつぎのようになっています。

13.1. Webページからデータを送信する 75

特殊記号をHTMLで正しく表示できる文字列に変換します。

1 def post(self):

2 content = cgi.escape(self.request.get('content'))

3 self.response.write('New Todo:' + content)

main.py

main.pyは次のような内容になります。

1 # coding: utf-8

-*-2 #!/usr/bin/env python

3 #

4 # Copyright 2007 Google Inc.

5 #

6 # Licensed under the Apache License, Version 2.0 (the "License");

7 # you may not use this file except in compliance with the License.

8 # You may obtain a copy of the License at

9 #

10 # http://www.apache.org/licenses/LICENSE-2.0

11 #

12 # Unless required by applicable law or agreed to in writing, software

13 # distributed under the License is distributed on an "AS IS" BASIS,

14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

15 # See the License for the specific language governing permissions and

16 # limitations under the License.

17 #

18 import cgi

19 import os

20 from google.appengine.api import users

21 import jinja2

22 import webapp2

23 import logging

24

25 JINJA_ENVIRONMENT = jinja2.Environment(

26 loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),

27 extensions=['jinja2.ext.autoescape'],

28 autoescape=True)

29 30

31 class MainHandler(webapp2.RequestHandler):

32 def get(self):

33 self.response.write('<html><body>')

34 self.response.write('<h1>TODOリスト</h1>')

35 self.response.write('<form method="post" action="/">')

36 self.response.write('<input type="text" name="task">')

7413章 フォームの作成

37 self.response.write('<input type="submit" value="送信" />')

38 self.response.write('</form>')

39 self.response.write('</body></html>')

40

41 def post(self):

42 task = cgi.escape(self.request.get('task'))

43 self.response.write('New Todo:' + task)

44 45 46

47 class MyPageHandler(webapp2.RequestHandler):

48 def get(self):

49 user = users.get_current_user()

50 if user:

51 self.response.headers['Content-Type'] = 'text/plain'

52 self.response.write('Hello, ' + user.nickname())

53 else:

54 self.redirect(users.create_login_url(self.request.uri))

55 56

57 def handle_404(request, response, exception):

58 logging.exception(exception)

59 response.write('ページが見つかりませんでした。')

60 response.set_status(404)

61 62

63 app = webapp2.WSGIApplication([

64 ('/', MainHandler),

65 ('/mypage', MyPageHandler)

66 ], debug=True)

67

68 app.error_handlers[404] = handle_404

確認

http://localhost:8080にアクセスするとつぎのようになっています。

13.1. Webページからデータを送信する 75