代码之家  ›  专栏  ›  技术社区  ›  iamrameshkumar

Python:Django TypeError:object()不接受任何参数

  •  6
  • iamrameshkumar  · 技术社区  · 6 年前

    我正在将一个django应用程序从1.x移植到2.1,遇到了一个错误:“TypeError:object()不带参数”。我试图解决这个问题已经有一段时间了,但即使在网上调试和搜索了好几天之后,我仍然没有得到任何线索

    已安装的应用程序:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.github',
        'timezone_field',
        'axes',
        'humans',
        'boxes',
        'pages',
    ]
    

    中间件设置:

    MIDDLEWARE = [
        'whitenoise.middleware.WhiteNoiseMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.security.SecurityMiddleware',
    ]
    

    压痕没有问题,


    raven版本:6.9.0
    django版本:2.1

    import os
    from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
    application=Sentry(get_wsgi_application())
    

    下面是错误日志的摘录

    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
            return _bootstrap._gcd_import(name[level:], package, level)   
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import   
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load   
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked  
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked   
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module   
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed   
    File "/app/wsgi.py", line 16, in <module>
            application=Sentry(get_wsgi_application())   
    File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
            return WSGIHandler()   
    File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 136, in __init__
            self.load_middleware()   
    File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 36, in load_middleware
            mw_instance = middleware(handler)
    TypeError: object() takes no parameters
    

    in <module>
        application = CustomSentry(get_wsgi_application())
      File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
        return WSGIHandler()
      File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 136, in __init__
        self.load_middleware()
      File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 36, in load_middleware
        mw_instance = middleware(handler)
    TypeError: object() takes no parameters
    

    我尝试使用ExceptionMiddleware捕获异常,现在出现以下错误:

    application = CustomSentry(get_wsgi_application())
    File "/usr/local/lib/python3.5/dist-packages/django/utils/deprecation.py", line 85, in __init__
        super().__init__()
    TypeError: __init__() missing 1 required positional argument: 'application'
    

    2 回复  |  直到 6 年前
        1
  •  9
  •   Daniel Hepper    6 年前

    该错误表示您的中间件列表中有一个旧式中间件。旧式中间件在实例化时不接收参数,而新式中间件接收处理程序。

    现在,根据您的设置,唯一的非Django中间件是whitenoise,但是您说即使在注释掉它之后错误仍然存在。

    • 如前所述,在Django源代码中添加一个断点或print语句,以找出导致问题的中间件。

    • 确保正在编辑的设置文件是实际使用的文件。

    • 使用pythonshell检查 MIDDLEWARE 设置:

      $ python manage.py shell
      >>> from django.conf import settings
      >>> settings.MIDDLEWARE
      ...
      
        2
  •  4
  •   JPG    6 年前

    /app/wsgi.py 模块,

    import os
    from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
    
    from django.core.wsgi import get_wsgi_application
    from django.utils.deprecation import MiddlewareMixin
    
    
    class CustomSentry(MiddlewareMixin, Sentry):
        pass
    
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
    application = CustomSentry(get_wsgi_application())




    1 object() takes no parameters in django 1.10
    2 Django exception middleware: TypeError: object() takes no parameters