ERROR __init __()在带有GAE的Django上只接受1个参数(3个给定)

时间:2016-10-10 15:00:23

标签: python python-2.7 google-app-engine webapp2

我试图解决我在一个Django App的管理员身上遇到的一个问题。

我有这段代码:

admin_main.py:

application = webapp.WSGIApplication([
# Admin pages
(r'^(/admin/add_img)', admin.views.AddImage),
(r'^(/admin)(.*)$', admin.Admin),])

管理员/ views.py:

class BaseRequestHandler(webapp.RequestHandler):
def handle_exception(self, exception, debug_mode):
    logging.warning("Exception catched: %r" % exception)
    if isinstance(exception, Http404) or isinstance(exception, Http500):
        self.error(exception.code)
        path = os.path.join(ADMIN_TEMPLATE_DIR, str(exception.code) + ".html")
        self.response.out.write(template.render(path, {'errorpage': True}))
    else:
        super(BaseRequestHandler, self).handle_exception(exception, debug_mode)

class Admin(BaseRequestHandler):

def __init__(self, request,response):
    logging.info("NEW Admin object created")
    super(Admin, request, response).__init__()
    # Define and compile regexps for Admin site URL scheme.
    # Every URL will be mapped to appropriate method of this
    # class that handles all requests of particular HTTP message
    # type (GET or POST).
    self.getRegexps = [
        [r'^/?$', self.index_get],
        [r'^/([^/]+)/list/$', self.list_get],
        [r'^/([^/]+)/new/$', self.new_get],
        [r'^/([^/]+)/edit/([^/]+)/$', self.edit_get],
        [r'^/([^/]+)/delete/([^/]+)/$', self.delete_get],
        [r'^/([^/]+)/get_blob_contents/([^/]+)/([^/]+)/$', self.get_blob_contents],
    ]
    self.postRegexps = [
        [r'^/([^/]+)/new/$', self.new_post],
        [r'^/([^/]+)/edit/([^/]+)/$', self.edit_post],
    ]
    self._compileRegexps(self.getRegexps)
    self._compileRegexps(self.postRegexps)
    # Store ordered list of registered data models.
    self.models = model_register._modelRegister.keys()
    self.models.sort()
    # This variable is set by get and port methods and used later
    # for constructing new admin urls.
    self.urlPrefix = ''

def index_get(self):
    """Show admin start page
    """
    path = os.path.join(ADMIN_TEMPLATE_DIR, 'index.html')
    self.response.out.write(template.render(path, {
        'models': self.models,
        'urlPrefix': self.urlPrefix,
    }))

当我尝试获取页面http://localhost:8080/admin时,我收到下一个错误:

    ERROR    2016-10-10 14:37:25,484 webapp2.py:1528] __init__() takes exactly 1 argument (3 given)
Traceback (most recent call last):
  File "C:\Users\Yisus-MSI\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\Yisus-MSI\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\Yisus-MSI\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Users\Yisus-MSI\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1076, in __call__
    handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)

我尝试了很多论坛的解决方案,但没有人工作。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我认为您在使用super()时犯了一个错误,您需要更改Admin.__init__()方法:

super(Admin, request, response).__init__()

为:

super(Admin, self).__init__(request=request, response=response)

答案 1 :(得分:0)

这应该有效:

    def __init__(self, *args, **kwargs):
          super(BaseApiHandler, self).__init__(*args, **kwargs)

并获得“请求”或“响应”,您需要:

    self.request...
    self.response..