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 Working in Django 1.10 #17

Closed dannyboscan closed 7 years ago

dannyboscan commented 7 years ago

Fix #13

kane-c commented 7 years ago

It would be great to see this merged and released.

In the mean time, you can write your own middleware that does the same thing:

from django.utils.functional import SimpleLazyObject
from django_user_agents.utils import get_user_agent

def user_agent_middleware(get_response):
    def middleware(request):
        request.user_agent = SimpleLazyObject(lambda: get_user_agent(request))

        return get_response(request)

    return middleware
digi0ps commented 7 years ago

I have it installed from pip. Now how do I make the necessary changes for the middleware to work? I am not able to locate the django_user_agents/middleware.py

dannyboscan commented 7 years ago

@digi0ps if you are using Django 1.10 just create your own Middlaware Class and references in MIDDLEWARE settings instead of django_user_agents.middleware.UserAgentMiddleware

Custom middleware:

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',
]
digi0ps commented 7 years ago

@dannyboscan Thanks a lot, it worked. 👍

selwin commented 7 years ago

Sorry for the delayed response. Does this PR still fixes problems on Django 1.10 and 1.11 or do we need to consider anoher approach? I can make a PyPI release sometime this week if we have a reliable fix for newer versions of Django.

dannyboscan commented 7 years ago

Still work in Django 1.11