Django - 无法添加中间件

时间:2017-04-16 17:22:49

标签: python django

我在我的应用程序中添加了一个中间件django-user-agents,用于获取用户代理信息,现在我的应用程序无法启动。我按照github页面上提到的所有步骤进行了操作:django_user_agents  但是不能让这件事起作用。

这是堆栈跟踪:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000000000520B048>
Traceback (most recent call last):
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\management\commands\runserver.py", line 142, in inner_run
    handler = self.get_handler(*args, **options)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super(Command, self).get_handler(*args, **options)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\management\commands\runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\servers\basehttp.py", line 49, in get_internal_wsgi_application
    return import_string(app_path)
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\utils\module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\catch\PycharmProjects\Backend_Noticeboard\noticeboard\noticeboard\wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\handlers\wsgi.py", line 153, in __init__
    self.load_middleware()
  File "C:\Users\catch\AppData\Roaming\Python\Python34\site-packages\django\core\handlers\base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

如果有人想出来的话会非常有用。

如果我错过了某些内容/某些内容不明确,请随意提出问题。

感谢期待!! :)

1 个答案:

答案 0 :(得分:1)

您尝试使用的中间件似乎与Django版本&gt; = 1.10不兼容。 GitHub已经存在一个未解决的问题:

https://github.com/selwin/django-user_agents/issues/13

他们现在的解决方法是尽可能使用旧式MIDDLEWARE_CLASSES设置而不是MIDDLEWARE设置。

相关拉取请求(https://github.com/selwin/django-user_agents/pull/17)的另一个选项是创建扩展其他中间件的自己的中间件:

from django.utils.deprecation import MiddlewareMixin
from django_user_agents.middleware import UserAgentMiddleware

class CustomUserAgentMiddleware(MiddlewareMixin, UserAgentMiddleware):
    pass

然后在你的settings.py中:

MIDDLEWARE = [
    # other middlewares...
    'path.to.your.own.file.CustomUserAgentMiddleware',
]