Package: django-user-agents
Django User AgentsA 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. Under the hood,
it usesuser-agents.InstallationInstalldjango-user-agents, you’ll have to make sure thatuser-agentsis installed first:pip install pyyaml ua-parser user-agents
pip install django-user-agentsConfiguresettings.py:INSTALLED_APPS=(# Other apps...'django_user_agents',)# Cache backend is optional, but recommended to speed up user agent parsingCACHES={'default':{'BACKEND':'django.core.cache.backends.memcached.MemcachedCache','LOCATION':'127.0.0.1:11211',}}# Name of cache backend to cache user agents. If it not specified default# cache alias will be used. Set to `None` to disable caching.USER_AGENTS_CACHE='default'UsageMiddlewareAddUserAgentMiddlewareinsettings.py:MIDDLEWARE_CLASSES=(# other middlewares...'django_user_agents.middleware.UserAgentMiddleware',)Auser_agentattribute will now be added torequest, which you can use
inviews.py:defmy_view(request):# Let's assume that the visitor uses an iPhone...request.user_agent.is_mobile# returns Truerequest.user_agent.is_tablet# returns Falserequest.user_agent.is_touch_capable# returns Truerequest.user_agent.is_pc# returns Falserequest.user_agent.is_bot# returns False# Accessing user agent's browser attributesrequest.user_agent.browser# returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')request.user_agent.browser.family# returns 'Mobile Safari'request.user_agent.browser.version# returns (5, 1)request.user_agent.browser.version_string# returns '5.1'# Operating System propertiesrequest.user_agent.os# returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')request.user_agent.os.family# returns 'iOS'request.user_agent.os.version# returns (5, 1)request.user_agent.os.version_string# returns '5.1'# Device propertiesrequest.user_agent.device# returns Device(family='iPhone')request.user_agent.device.family# returns 'iPhone'If you havedjango.core.context_processors.requestenabled,user_agentwill also be available in template throughrequest:{%ifrequest.user_agent.is_mobile%}Do stuff here...{%endif%}View Usagedjango-user_agentscomes withget_user_agentwhich takes a singlerequestargument and returns aUserAgentinstance. Example usage:fromdjango_user_agents.utilsimportget_user_agentdefmy_view(request):user_agent=get_user_agent(request)ifuser_agent.is_mobile:# Do stuff here...elifuser_agent.is_tablet:# Do other stuff...Template Usagedjango-user_agentscomes with a few template filters:is_mobileis_tabletis_touch_capableis_pcis_botYou can use all of these like any other django template filters:{%loaduser_agents%}{%ifrequest|is_mobile%}Mobile device stuff...{%endif%}{%ifrequest|is_tablet%}Tablet stuff...{%endif%}{%ifrequest|is_pc%}PC stuff...{%endif%}{%ifrequest|is_touch_capable%}Touch capable device stuff...{%endif%}{%ifrequest|is_bot%}Bot stuff...{%endif%}You can find out more about user agent attributes athere.Running Tests`which django-admin.py` test django_user_agents --settings=django_user_agents.tests.settings --pythonpath=.Changelog0.4.0Add support for Django 2.0 to 2.2. Thanks @adamchainz and @joehybird!0.3.1Fixed a bug when request have no META attribute0.3.0Python 3, thanks to @hwkns!0.2.2Fixed a bug that causes cache set/read to fail when user agent is longer than 250 characters0.2.1Fixed packaging0.2.0Added template filtersAddedget_user_agentfunction in utils.py0.1.1Fixed aKeyErrorexception in the case of emptyHTTP_USER_AGENT0.1Initial release