Life is Simple

  • Random
  • Archive

Acoustic theme by Rolando Murillo, using the iPhone toolbar icons.

  • Permalink

    既存のエンティティの編集

    ここでは、作成したサンプル アプリケーションを拡張して、買い物リストのユーザーが買い物リストの既存のエントリを編集できるようにします。

    ItemPage クラスの変更

    まず、ItemPage のリクエストを処理する際に、エントリを編集するためのリンクを各項目の横に追加します。このリンクは /edit URL を要求し、項目の id をクエリの引数として含めます。

    class ItemPage(webapp.RequestHandler):
     
    def get(self):
        query
    = db.GqlQuery("SELECT * FROM Item ORDER BY name")
       
    for item in query:
         
    self.response.out.write('<a href="/edit?id=%d">Edit</a> - ' %
                                  item
    .key().id())
         
    self.response.out.write("%s - Need to buy %d, cost $%0.2f each<br>" %
                                 
    (item.name, item.quantity, item.target_price))

    EditPage クラスの追加

    次に、編集リクエストを処理する EditPage クラスを追加する必要があります。このハンドラは、項目の追加に使用するハンドラによく似ています。

    EditPage クラスの get() メソッドでは、まず編集するインスタンスをデータストアから取得し、そのインスタンスをフォーム レンダラに渡して、項目の情報が入力できるようにします。また、項目の id を非表示入力としてフォームに追加して、編集した項目をユーザーが送信したときに、アプリケーションが id 情報にアクセスできるようにします。

    class EditPage(webapp.RequestHandler):
     
    def get(self):
        id = int(self.request.get('id'))
        item
    = Item.get(db.Key.from_path('Item', id))

       
    self.response.out.write('<html><body>'
                               
    '<form method="POST" '
                                'action="/edit">'
                               
    '<table>')
       
    self.response.out.write(ItemForm(instance=item))
       
    self.response.out.write('</table>'
                                '<input type="hidden" name="_id" value="%s">'
                               
    '<input type="submit">'
                               
    '</form></body></html>' % id)

    次に、HTTP POST を処理するメソッドを作成します。編集している項目をデータストアで検索してから、Django フォームで利用可能な検証フレームワークを使用して、編集された情報を検証します。すでに説明した方法で、情報が有効であればその変更をデータストアに保 存し、有効でなければその入力をユーザーが修正できるようにします。

      def post(self):
        id = int(self.request.get('_id'))
        item
    = Item.get(db.Key.from_path('Item', id))

        data
    = ItemForm(data=self.request.POST, instance=item)
       
    if data.is_valid():
         
    # Save the data, and redirect to the view page
          entity
    = data.save(commit=False)
          entity
    .added_by = users.get_current_user()
          entity
    .put()
         
    self.redirect('/items.html')
       
    else:
         
    # Reprint the form
         
    self.response.out.write('<html><body>'
                                 
    '<form method="POST" '
                                  'action="/edit">'
                                 
    '<table>')
         
    self.response.out.write(data)
         
    self.response.out.write('</table>'
                                  '<input type="hidden" name="_id" value="%s">'
                                 
    '<input type="submit">'
                                 
    '</form></body></html>' % id)

    main() 関数の変更

    最後に、main 関数に新しい URL とリクエスト ハンドラを追加する必要があります。

    def main():
      application
    = webapp.WSGIApplication(
                                           
    [('/', MainPage),
                                            ('/edit', EditPage),
                                           
    ('/items.html', ItemPage),
                                           
    ],
                                           debug
    =True)

      wsgiref
    .handlers.CGIHandler().run(application)

    — Google App Engine での Django フォームの検証 - Google App Engine - Google Code