Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
django-user-agents
A django package that allows easy identification of visitors' browser, operating system and device information (mobile phone, tablet or has touch capabilities).
Readme
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 <https://github.com/selwin/python-user-agents>
_.
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
Configure settings.py
:
.. code-block:: python
INSTALLED_APPS = ( # Other apps... 'django_user_agents', )
CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } }
None
to disable caching.USER_AGENTS_CACHE = 'default'
Add UserAgentMiddleware
in settings.py
:
.. code-block:: python
MIDDLEWARE_CLASSES = (
# other middlewares...
'django_user_agents.middleware.UserAgentMiddleware',
)
A user_agent
attribute will now be added to request
, which you can use
in views.py
:
.. 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')
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 properties
request.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 properties
request.user_agent.device # returns Device(family='iPhone')
request.user_agent.device.family # returns 'iPhone'
If you have django.core.context_processors.request
enabled, user_agent
will also be available in template through request
:
.. code-block:: html+django
{% if request.user_agent.is_mobile %}
Do stuff here...
{% endif %}
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...
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:
.. code-block:: html+django
{% 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 <https://github.com/selwin/python-user-agents>
_.
.. code-block:: console
`which django-admin.py` test django_user_agents --settings=django_user_agents.tests.settings --pythonpath=.
get_user_agent
function in utils.pyKeyError
exception in the case of empty HTTP_USER_AGENT
FAQs
A django package that allows easy identification of visitors' browser, operating system and device information (mobile phone, tablet or has touch capabilities).
We found that django-user-agents demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.