gpt4 book ai didi

python - Django - 无法添加中间件

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:16 24 4
gpt4 key购买 nike

我在应用程序中添加了一个中间件 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

如果有人能解决这个问题,那将会非常有帮助。

如果我错过了什么/有些不清楚,请随时提出问题。

谢谢期待!! :)

最佳答案

您尝试使用的中间件似乎与 Django 版本 >= 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',
]

关于python - Django - 无法添加中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com