简体   繁体   English

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

[英]Python : Django TypeError: object() takes no parameters

I'm porting a django application from 1.x to 2.1 and got stuck with the error that says "TypeError: object() takes no parameters". 我正在将一个django应用程序从1.x移植到2.1,并且遇到了错误,即“TypeError:object()不带参数”。 I'm trying to solve the problem for quite a while but not even got a clue even after days of debugging and searching online 我试图解决这个问题很长一段时间,但即使经过数天的调试和在线搜索,也没有得到任何线索

Installed apps: 已安装的应用:

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 settings: 中间件设置:

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',
]

There are no problems with the indentation, 压痕没有问题,

celery version : 4.2.1 芹菜版:4.2.1
raven version : 6.9.0 乌鸦版:6.9.0
django version : 2.1 django版本:2.1

Here is my wsgi.py 这是我的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())

Here is an excerpt from the error log 以下是错误日志的摘录

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

Error after using CustomSentry : 使用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

I tried to catch the exceptions using ExceptionMiddleware, now I'm getting the following error: 我试图使用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'

Any help would be appreciated. 任何帮助,将不胜感激。

The error indicates that you have an old-style middleware in your middleware list. 该错误表明您的中间件列表中有旧式中间件。 Old-style middlewares did not receive an argument upon instantiation, while new-style middlewares receive a handler. 旧式中间件在实例化时没有收到论据,而新式中间件则收到处理程序。

Now, according to your settings, the only non-Django middleware is whitenoise, but you say that the error persists even after commenting that out. 现在,根据您的设置,唯一的非Django中间件是whitenoise,但是你说这个错误在评论之后仍然存在。

Here are some suggestion to help you figure out what's going on: 这里有一些建议可以帮助你弄清楚发生了什么:

  • As I have commented, add a breakpoint or print statement to the Django source to figure out which middleware causes the issue. 正如我所评论的那样,向Django源添加断点或print语句以找出导致问题的中间件。

  • Make sure that the settings file you are editing is the one that is actually used. 确保您正在编辑的设置文件是实际使用的设置文件。

  • Use the Python shell to inspect the actual value of the MIDDLEWARE setting: 使用Python shell检查MIDDLEWARE设置的实际值:

     $ python manage.py shell >>> from django.conf import settings >>> settings.MIDDLEWARE ... 

try this in you /app/wsgi.py module, 在你的/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())



References 参考
1. object() takes no parameters in django 1.10 1. object()在django 1.10中没有参数
2. Django exception middleware: TypeError: object() takes no parameters 2. Django异常中间件:TypeError:object()不带参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM