Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

A django package that allows easy identification of visitors' browser, operating system and device information (mobile phone, tablet or has touch capabilities).

Project Description
Public version 0.3.1

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. Under the hood,
it uses `user-agents <>`_.


1. Install ``django-user-agents``, you'll have to make sure that `user-agents`_ is installed first::

pip install pyyaml ua-parser user-agents
pip install django-user-agents

2. Configure ````:

.. code-block:: python

# Other apps...

# Cache backend is optional, but recommended to speed up user agent parsing
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',

# Name of cache backend to cache user agents. If it not specified default
# cache alias will be used. Set to `None` to disable caching.


Add ``UserAgentMiddleware`` in ````:

.. code-block:: python

# other middlewares...

A ``user_agent`` attribute will now be added to ``request``, which you can use
in ````:

.. code-block:: python

def my_view(request):

# Let's assume that the visitor uses an iPhone...
request.user_agent.is_mobile # returns True
request.user_agent.is_tablet # returns False
request.user_agent.is_touch_capable # returns True
request.user_agent.is_pc # returns False
request.user_agent.is_bot # returns False

# Accessing user agent's browser attributes
request.user_agent.browser # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1') # returns 'Mobile Safari'
request.user_agent.browser.version # returns (5, 1)
request.user_agent.browser.version_string # returns '5.1'

# Operating System properties
request.user_agent.os # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1') # returns 'iOS'
request.user_agent.os.version # returns (5, 1)
request.user_agent.os.version_string # returns '5.1'

# Device properties
request.user_agent.device # returns Device(family='iPhone') # returns 'iPhone'

If you have ``django.core.context_processors.request`` enabled, ``user_agent``
will also be available in template through ``request``::

{% if request.user_agent.is_mobile %}
Do stuff here...
{% endif %}

View Usage

``django-user_agents`` comes with ``get_user_agent`` which takes a single
``request`` argument and returns a ``UserAgent`` instance. Example usage:

.. code-block:: python

from django_user_agents.utils import get_user_agent

def my_view(request):
user_agent = get_user_agent(request)
if user_agent.is_mobile:
# Do stuff here...
elif user_agent.is_tablet:
# Do other stuff...

Template Usage

``django-user_agents`` comes with a few template filters:

* ``is_mobile``
* ``is_tablet``
* ``is_touch_capable``
* ``is_pc``
* ``is_bot``

You can use all of these like any other django template filters::

{% load user_agents %}

{% if request|is_mobile %}
Mobile device stuff...
{% endif %}

{% if request|is_tablet %}
Tablet stuff...
{% endif %}

{% if request|is_pc %}
PC stuff...
{% endif %}

{% if request|is_touch_capable %}
Touch capable device stuff...
{% endif %}

{% if request|is_bot %}
Bot stuff...
{% endif %}

You can find out more about user agent attributes at `here <>`_.

Running Tests

.. code-block:: console

`which` test django_user_agents --settings=django_user_agents.tests.settings --pythonpath=.


* Fixed a bug when request have no META attribute

* Python 3, thanks to @hwkns!

* Fixed a bug that causes cache set/read to fail when user agent is longer than 250 characters

* Fixed packaging

* Added template filters
* Added ``get_user_agent`` function in

* Fixed a ``KeyError`` exception in the case of empty ``HTTP_USER_AGENT``

* Initial release
Release History

Release History

This version
History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-user-agents-last-0.3.1.tar.gz (7.0 kB) Copy SHA256 Checksum SHA256 Source Jul 26, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting