Python:Django TypeError:object()不带参数

时间:2018-08-30 20:11:09

标签: python django python-3.x django-rest-framework python-3.5

我正在将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',
]

缩进没有问题,

  

芹菜版本:4.2.1
  乌鸦版本:6.9.0
  django版本:2.1

这是我的wsgi.py

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

使用CustomSentry后出现错误:

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 个答案:

答案 0 :(得分:8)

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

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

这里有一些建议可以帮助您了解正在发生的事情:

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

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

  • 使用Python shell检查mapPage.js设置的实际值:

    MIDDLEWARE

答案 1 :(得分:5)

在您的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}}



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