我尝试在Django中编写中间件。我得到了错误,我不知道为什么。以下是代码: setting.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'my.middleware.ReadedMiddleware'
]
中间件:
from django.utils.deprecation import MiddlewareMixin
class ReadedMiddleware(MiddlewareMixin):
def process_request(self,req):
pass
错误信息:
mw_instance =中间件(处理程序)
TypeError:object()不带参数
根据其他答案。如果中间件子类来自middlewareMixin而不是object,则错误将被解决。但它没有用。
以下是完整错误:
Unhandled exception in thread started by
<_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at
0x7f56cc6580f0>
Traceback (most recent call last):
File "/home/lyg/pycharm-
2017.3.3/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 589, in
__call__
return self.original_func(*self.args, **self.kwargs)
File "/home/lyg/.local/lib/python3.5/site-
packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/management/commands/runserver.py", line 140, in
inner_run
handler = self.get_handler(*args, **options)
File "/home/lyg/.local/lib/python3.5/site-
packages/django/contrib/staticfiles/management/commands/runserver.py",
line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/management/commands/runserver.py", line 65, in
get_handler
return get_internal_wsgi_application()
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/servers/basehttp.py", line 44, in
get_internal_wsgi_application
return import_string(app_path)
File "/home/lyg/.local/lib/python3.5/site-
packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
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 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 697, in
exec_module
File "<frozen importlib._bootstrap>", line 222, in
_call_with_frames_removed
File "/home/lyg/PycharmProjects/my/my/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/handlers/wsgi.py", line 140, in __init__
self.load_middleware()
File "/home/lyg/.local/lib/python3.5/site-
packages/django/core/handlers/base.py", line 39, in load_middleware
mw_instance = middleware(handler)
TypeError: object() takes no parameters