selwin / django-user_agents

A django package that allows easy identification of visitor's browser, OS and device information, including whether the visitor uses a mobile phone, tablet or a touch capable device.
MIT License
640 stars 104 forks source link

UserAgentMiddleware is not working in Django 1.10 #13

Closed adrysn closed 7 years ago

adrysn commented 8 years ago

UserAgentMiddleware is not working with Django 1.10. If it is inserted into the list of middlewares in settings file, an error occurs such as below.

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10e19c158>
Traceback (most recent call last):
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 142, in inner_run
    handler = self.get_handler(*args, **options)
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super(Command, self).get_handler(*args, **options)
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/servers/basehttp.py", line 49, in get_internal_wsgi_application
    return import_string(app_path)
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/Users/adrysn/.pyenv/versions/3.5.0/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/adrysn/lablup/neumann/neumann/wsgi.py", line 17, in <module>
    application = get_wsgi_application()
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 153, in __init__
    self.load_middleware()
  File "/Users/adrysn/.pyenv/versions/neumann35/lib/python3.5/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

This error is probably due to the changed style of middleware introduced in Django 1.10: https://docs.djangoproject.com/en/1.10/topics/http/middleware/.

The middleware class should be updated to comply with the new style.

For the moment, one can replace object to MiddlewareMixin to enable the middleware.

from django.utils.deprecation import MiddlewareMixin

class UserAgentMiddleware(MiddlewareMixin):
...
rocchidavide commented 8 years ago

So how can I solve it?

maxim-kht commented 7 years ago

Current solution would be to keep using old style middleware settings https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-MIDDLEWARE_CLASSES

MIDDLEWARE_CLASSES = [
    ...
    'django_user_agents.middleware.UserAgentMiddleware',
]

It will be removed in the future Django versions though.

dmwyatt commented 7 years ago

You can do this to use it in the new MIDDLEWARE style:

class NewUserAgentMiddleware(MiddlewareMixin, UserAgentMiddleware):
    pass

Then just reference that class in MIDDLEWARE.