【问题标题】:Is it possible to use the same method for both get and post with webapp2?是否可以对 webapp2 的 get 和 post 使用相同的方法?
【发布时间】:2014-09-13 23:18:53
【问题描述】:

我的 get 和 post 方法几乎没有区别。一种方法是将通用逻辑放在另一个函数中,并在 get 和 post 方法中调用它。但在我这样做之前,我想知道如果我真的可以让一个函数同时处理两者,那将非常整洁。

【问题讨论】:

  • 您可以使 post call get 或反之亦然。只需执行 self.get() 或 self.post()

标签: python google-app-engine webapp2


【解决方案1】:

您也可以为您的处理程序使用 BaseHandler。您可以将会话、登录和模板的常用方法放在 BaseHandler 中。

有关会话,请参阅 this example 或有关 webapp2 和模板的 this blog post

【讨论】:

    【解决方案2】:

    This 很好地描述了何时使用 GET 与 POST。当然,您可以使用其中任何一种,但在某些情况下您希望使用一种与另一种。如果您愿意,可以使用相同的方法在同一类中处理它们:

    class MyHandler(webapp2.RequestHandler): 
    
        def function_to_handle_requests(self):
            # code goes here
    
        def get(self): 
            self.function_to_handle_requests
    
        def post(self):
            self.function_to_handle_requests
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多