3

给定这个简单的类:

class RenderResponseHandler(webapp2.RequestHandler):
    def __init__(self):
        self.nombreUsuario = obtenerUsuarioTablaUsuario().nombre if obtenerUsuarioTablaUsuario() else None
    def doRender(self, tname='index.html',values = {}):
        temp = os.path.join(os.path.dirname(__file__),
                            'templates/'+tname)
        if not os.path.isfile(temp):
            return False

        self.nombreUsuario = obtenerUsuarioTablaUsuario().nombre if obtenerUsuarioTablaUsuario() else None

        newval = dict(values)
        newval['path'] = self.request.path

        if nombreUsuario:
            url_linktext = 'logout'
        else:
            url_linktext = 'login'

        newval['url_linktext'] = url_linktext
        newval['nombreUsuario'] = nombreUsuario

        template = jinja_environment.get_template(tname)
        outstr = template.render(newval)
        self.response.out.write(outstr)

        return True

    def obtenerPagina(self, pOpcion = None, pMensajeInformacion = None):
        opcion = pOpcion if pOpcion is not None else self.request.get('opcion')
        if opcion == 'index':
            self.doRender('index.html')
        ####

如果我这样做,页面会正常显示

class MainPage(webapp2.RequestHandler):
    def get(self):
        r = RenderResponseHandler()
        r.request = self.request
        r.response = self.response 
        r.obtenerPagina('index')

虽然,当尝试从 RenderResponseHandler 继承时,像这样:

class MainPage(RenderResponseHandler):
    def get(self):
        if self.doRender(self.request.path):
            return
        self.obtenerPagina('index') 

我得到以下异常:

Traceback (most recent call last):   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1536, in
__call__
    rv = self.handle_exception(request, response, e)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1530, in
__call__
    rv = self.router.dispatch(request, response)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)   File "/home/lucas/Dropbox/Tesis/Revision 2012/Desarrollo/Entorno/AppEngine SDK/google_appengine_1.6.6/lib/webapp2/webapp2.py", line 1101, in
__call__
    handler = self.handler(request, response) TypeError: __init__() takes exactly 1 argument (3 given)

我究竟做错了什么?

提前致谢!

4

2 回答 2

6

您需要__init__像这样实现 RenderResponseHandler :def __init__(self, request=None, response=None)并将请求和响应传递给基类。

调用请求处理程序类的层在 ctor 将具有这些参数的合同上中继。

于 2012-06-09T05:38:25.193 回答
0

如下所示;我们可以从其他 Google App Engine 类调用 myfunc 函数,而无需在类定义行中声明。_ init _函数让 GAE 感到困惑,因为他的初始函数是 get()

class Test2(webapp2.RequestHandler):
    def get(self):
          tx=Test()
          self.response.out.write(tx.myfunc())


class Test(webapp2.RequestHandler):
  def myfunc(self):
      return "Hooooooooooooooop"
  def get (self,request=None, response=None):
      self.response.out.write("test2")
于 2013-09-14T10:59:43.777 回答