Django exception middleware: TypeError: object() takes no parameters

升级jiango过程中,修改settings.py后完整报错信息如下:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10fa4a840>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 140, in inner_run
    handler = self.get_handler(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 65, in get_handler
    return get_internal_wsgi_application()
  File "/usr/local/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 44, in get_internal_wsgi_application
    return import_string(app_path)
  File "/usr/local/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/fukainan/Desktop/jiango-blank/webapp/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 140, in __init__
    self.load_middleware()
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

查看报错信息将错误定位于middleware。
查找stackoverflow后发现是Django新版本更改了自定义middleware的写法:
https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
但settings.py中并未启用自定义middleware,如下:

MIDDLEWARE = [
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

在settings.py中查找middleware后发现下面这段代码:

if DEBUG:
    MIDDLEWARE = list(MIDDLEWARE)
    MIDDLEWARE.append('jiango.debug.middleware.SQLDebugMiddleware')

更改SQLDebugMiddleware后问题解决。

    原文作者:傅恺男
    原文地址: https://www.jianshu.com/p/8d5ebfc090f7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞