Skip to main content
 首页 » 编程设计

python之你如何从 python appengine 中的 basehandler 调用处理程序

2025年05月04日27mq0036

我想做这样的事情:

class Basehandler(webapp.RequestHandler): 
 
  def __init__(self): 
    if checkforspecialcase: #check something that always needs to be handled 
      return SpecialCaseHandler.get() 
 
class NormalHandler(Basehandler): 
 
  def get(self): 
    print 'hello world' 
    return 
 
class SpecialCaseHandler(Basehandler): 
 
  def get(self): 
    print 'hello special world' 
    return 

想法是,无论最初调用什么处理程序,如果满足某种情况,我们基本上都会切换到另一个处理程序。

我是 python 的新手,所以我不确定我尝试做的事情是否可行。或者这是否是最好的方法。我真正想做的是确保向某人展示完整的个人资料页面,如果他们已经开始注册过程但还没有完成……无论他们提出什么要求。因此,“checkforspecialcase”查看他们的 session 并检查不完整的信息。

请您参考如下方法:

要保持干燥,请使用 Template Method pattern

class BaseHandler(webapp.RequestHandler): 
    def DoGet(self, *args): 
        ''' defined in derived classes, actual per-handler get() logic''' 
        pass 
 
    def get(self, *args): 
        # don't get caught in endless redirects! 
        if specialCase and not self.request.path.startswith('/special'): 
            self.redirect('/special') 
        else: 
            self.DoGet(*args) 
 
class NormalHandler(BaseHandler): 
    def DoGet(self, *args): 
        # normal stuff 
 
class SpecialHandler(BaseHandler): 
    def DoGet(self, *args): 
        # SPECIAL stuff