Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 69.8MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.7s · import 0.000s · 68MB
68MB installed
● package 68MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
UserAgentMiddleware
✓ from django_user_agents.middleware import UserAgentMiddleware
get_user_agent
✓ from django_user_agents.utils import get_user_agent
To use `django-user-agents`, first add `django_user_agents` to your `INSTALLED_APPS` and `UserAgentMiddleware` to your `MIDDLEWARE` setting. Once configured, a `user_agent` attribute will be automatically added to the `request` object, providing properties like `is_mobile`, `is_tablet`, `is_pc`, and details about the browser and OS. Caching is optional but highly recommended for performance. You can also manually get a `UserAgent` instance using `get_user_agent(request)`.
# settings.py
INSTALLED_APPS = [
# ... other apps
'django_user_agents',
]
MIDDLEWARE = [
# ... other middleware
'django_user_agents.middleware.UserAgentMiddleware',
]
# Optional: Recommended for performance
# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
# 'LOCATION': 'unique-snowflake',
# }
# }
# USER_AGENTS_CACHE = 'default'
# views.py
from django.shortcuts import render
from django_user_agents.utils import get_user_agent
def my_view(request):
# Access via request.user_agent (if middleware is used)
is_mobile = request.user_agent.is_mobile
is_tablet = request.user_agent.is_tablet
is_pc = request.user_agent.is_pc
browser_family = request.user_agent.browser.family
# Alternatively, get it explicitly
# user_agent = get_user_agent(request)
# is_bot = user_agent.is_bot
context = {
'is_mobile': is_mobile,
'is_tablet': is_tablet,
'is_pc': is_pc,
'browser_family': browser_family,
}
return render(request, 'my_template.html', context)
Debug
Known issues
breakingFor Django versions 1.10 and later, you must use the `MIDDLEWARE` setting instead of the deprecated `MIDDLEWARE_CLASSES`. Failing to do so will prevent the `UserAgentMiddleware` from being active and `request.user_agent` will not be available.fixUpdate your `settings.py` to use `MIDDLEWARE = [...]` and place `'django_user_agents.middleware.UserAgentMiddleware'` within that list.
affects: < 0.4.0 (for older Django), all versions (if migrating old settings)
gotchaThe prerequisite packages `pyyaml`, `ua-parser`, and `user-agents` must be installed *before* `django-user-agents`. Not installing these dependencies first will lead to `ModuleNotFoundError` or other import errors when `django-user-agents` attempts to load them.fixEnsure you run `pip install pyyaml ua-parser user-agents` before `pip install django-user-agents`.
affects: All versions
gotchaCaching user agent parsing results is optional but strongly recommended for performance, especially under high traffic. Without caching configured, the library will re-parse the user agent string on every request, which can introduce unnecessary overhead.fixConfigure a cache backend in your `settings.py` (e.g., `django.core.cache.backends.locmem.LocMemCache` for simple setups) and set `USER_AGENTS_CACHE = 'your_cache_alias'`.
affects: All versions
gotchaUser-Agent strings are provided by the client and can be easily spoofed. Do not rely solely on `request.user_agent` properties for security-sensitive operations or critical business logic where precise, unforgeable client identification is required.fixComplement user-agent data with other security measures (e.g., IP blacklisting, robust authentication) for sensitive actions, or use it only for informational and analytics purposes.
affects: All versions
deprecatedThe `0.4.0` release was published in June 2019, officially supporting Python up to 3.7 and Django up to 2.2. While it might still function with newer Python (3.8+) or Django (3.0+) versions due to backward compatibility, official support and testing for these newer environments are not guaranteed. Users should test thoroughly for compatibility if using modern Python/Django.fixThoroughly test `django-user-agents` functionality in your specific Python/Django environment. Consider contributing to the project or looking for actively maintained forks if critical issues arise in newer environments.
affects: 0.4.0 (with Python 3.8+ or Django 3.0+)
Upgrade
Version history
0.4.0latest on PyPI · released Jun 22, 2019
Audit
Dependencies
DjangorequiredCore framework requirement for the package.
pyyamlrequiredRequired by 'user-agents' library, which is a dependency of django-user-agents.
ua-parserrequiredRequired by 'user-agents' library, which is a dependency of django-user-agents.
user-agentsrequiredThe underlying library used for parsing user agent strings.