2011-08-13 14 views
4

webapp2フレームワークを学習しています。このフレームワークは、強力なRouteメカニズムを使用しています。webapp2.オプションの先頭に

私のアプリケーションは、これらのようなURIを受け入れるようになっている:

/poll/abc-123 
/poll/abc-123/ 
/poll/abc-123/vote/  # post new vote 
/poll/abc-123/vote/456 # view/update a vote 

投票、必要に応じてカテゴリに分類することができるので、上記のすべてのようにも動作するはずです:

/mycategory/poll/abc-123 
/mycategory/poll/abc-123/ 
/mycategory/poll/abc-123/vote/ 
/mycategory/poll/abc-123/vote/456 

私の誤った構成:

app = webapp2.WSGIApplication([ 
    webapp2.Route('/<category>/poll/<poll_id><:/?>', PollHandler), 
    webapp2.Route('/<category>/poll/<poll_id>/vote/<vote_id>', VoteHandler), 
], debug=True) 

質問:設定をどのように修正できますか?

可能であれば、GAEのCPU時間/ホスティング料金に合わせて最適化する必要があります。たとえば、カテゴリごとに1行、カテゴリなしで別の行を2行追加するともっと速くなる場合があります。

答えて

7

webapp2には共通接頭辞を再利用する仕組みがありますが、

app = webapp2.WSGIApplication([ 
    webapp2.Route('/poll/<poll_id><:/?>', PollHandler), 
    webapp2.Route('/poll/<poll_id>/vote/<vote_id>', VoteHandler), 
    webapp2.Route('/<category>/poll/<poll_id><:/?>', PollHandler), 
    webapp2.Route('/<category>/poll/<poll_id>/vote/<vote_id>', VoteHandler), 
], debug=True) 

多くのルートを追加する心配はありません。彼らはビルドしてマッチするのに本当に安いです。あなたが何万人も持っていなければ、ルートの数を減らすことは重要ではありません。

小さなメモ:最初のルートはオプションの終了スラッシュを受け入れます。代わりに、strict_slash=Trueオプションを使用して、RedirectRouteを1つだけ受け入れ、リダイレクトする場合はリダイレクトすることができます。これは十分に文書化されていませんが、しばらくの間されています。 explanation in the docstringを参照してください。

+0

ありがとう、ロドリゴ! – zengabor

1

@moraesの上に無料の回答として私の解決策を追加します。
次のような問題を抱えている人は、より完全な回答を得ることができます。また

  • Optional Parameter Problem
  • Trailing Slash Problem
    1. は、私は1つの正規表現にどのように経路の両方 /entity/create/entity/edit/{id}を考え出しました。
      以下のURLパターンをサポートする私のルートがあります。

      1. /
      2. /myentities
      3. /myentities/
      4. /myentities/
      5. /myentities /作成/
      6. を作成
      7. /myentities /編集/ {ENTITY_ID}
      SITE_URLS = [ 
          webapp2.Route(r'/', handler=HomePageHandler, name='route-home'), 
      
          webapp2.Route(r'/myentities/<:(create/?)|edit/><entity_id:(\d*)>', 
           handler=MyEntityHandler, 
           name='route-entity-create-or-edit'), 
      
          webapp2.SimpleRoute(r'/myentities/?', 
           handler=MyEntityListHandler, 
           name='route-entity-list'), 
      ] 
      
      app = webapp2.WSGIApplication(SITE_URLS, debug=True) 
      

      以下は私のです私のすべてのハンドラが継承しています。以下は

      class BaseHandler(webapp2.RequestHandler): 
          @webapp2.cached_property 
          def jinja2(self): 
           # Sets the defaulte templates folder to the './app/templates' instead of 'templates' 
           jinja2.default_config['template_path'] = s.path.join(
            os.path.dirname(__file__), 
            'app', 
            'templates' 
           ) 
      
           # Returns a Jinja2 renderer cached in the app registry. 
           return jinja2.get_jinja2(app=self.app) 
      
          def render_response(self, _template, **context): 
           # Renders a template and writes the result to the response. 
           rv = self.jinja2.render_template(_template, **context) 
           self.response.write(rv) 
      

      Google App EngineのデータストアのAPIためget()メソッドシグネチャと私のMyEntityHandler Pythonのクラスです。

      class MyEntityHandler(BaseHandler): 
          def get(self, entity_id, **kwargs): 
           if entity_id: 
            entity = MyEntity.get_by_id(int(entity_id)) 
            template_values = { 
             'field1': entity.field1, 
             'field2': entity.field2 
            } 
           else: 
            template_values = { 
             'field1': '', 
             'field2': '' 
            } 
          self.render_response('my_entity_create_edit_view_.html', **template_values) 
      
      
      
          def post(self, entity_id, **kwargs): 
           # Code to save to datastore. I am lazy to write this code. 
      
           self.redirect('/myentities') 
      
    関連する問題