【问题标题】:ERROR __init__() takes exactly 1 argument (3 given) on Django with GAE错误 __init__() 在带有 GAE 的 Django 上恰好采用 1 个参数(给定 3 个)
【发布时间】:2017-02-19 01:09:44
【问题描述】:

我正在尝试解决我的一个 Django 应用程序管理员遇到的一个问题。

我有这个代码:

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)

我尝试了很多论坛的解决方案,但没有一个有效。

感谢您的帮助。

【问题讨论】:

  • 这似乎根本没有使用 Django。
  • 也许这不是 Django 的问题,但我不知道是什么问题@DanielRoseman

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


【解决方案1】:

我相信你在使用super()时犯了一个错误,你需要改变你的Admin.__init__()方法:

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

到:

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

【讨论】:

    【解决方案2】:

    这应该可行:

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

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

        self.request...
        self.response..
    

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2023-03-28
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 2019-04-05
      • 1970-01-01
      相关资源
      最近更新 更多