Divij Sehgal
Divij Sehgal

Reputation: 655

Django - Cannot add middleware

I added a middleware django-user-agents to my application, used for getting user agent information and now my app won't start up. I followed all steps mentioned on their github page: django_user_agents but cannot get this thing working.

Here's the stacktrace:

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

Would be really helpful if someone would figure this out.

Feel free to pop a question in case I missed something/something is not clear.

Thanks in anticipation!! :)

Upvotes: 1

Views: 673

Answers (1)

Jens
Jens

Reputation: 21540

The middleware you are trying to use seems not to be compatible with Django version >= 1.10. There is already an open issue on GitHub for your exact problem:

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

Their workaround right now is to use the old-style MIDDLEWARE_CLASSES setting instead of the MIDDLEWARE setting if possible.

Another option from the associated pull request (https://github.com/selwin/django-user_agents/pull/17) is to create your own middleware extending the other middleware:

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

class CustomUserAgentMiddleware(MiddlewareMixin, UserAgentMiddleware):
    pass

And then in your settings.py:

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

Upvotes: 1

Related Questions