{"id":5194,"library":"django-user-agents","title":"Django User Agents","description":"A Django package that allows easy identification of visitors' browser, operating system, and device information (mobile phone, tablet, or touch capabilities). It uses the `user-agents` library under the hood. The current version is 0.4.0.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/selwin/django-user_agents","tags":["django","user-agent","browser detection","device detection","middleware"],"install":[{"cmd":"pip install pyyaml ua-parser user-agents","lang":"bash","label":"Install core user-agent dependencies"},{"cmd":"pip install django-user-agents","lang":"bash","label":"Install django-user-agents"}],"dependencies":[{"reason":"Core framework requirement for the package.","package":"Django","optional":false},{"reason":"Required by 'user-agents' library, which is a dependency of django-user-agents.","package":"pyyaml","optional":false},{"reason":"Required by 'user-agents' library, which is a dependency of django-user-agents.","package":"ua-parser","optional":false},{"reason":"The underlying library used for parsing user agent strings.","package":"user-agents","optional":false}],"imports":[{"symbol":"UserAgentMiddleware","correct":"from django_user_agents.middleware import UserAgentMiddleware"},{"symbol":"get_user_agent","correct":"from django_user_agents.utils import get_user_agent"}],"quickstart":{"code":"# settings.py\nINSTALLED_APPS = [\n    # ... other apps\n    'django_user_agents',\n]\n\nMIDDLEWARE = [\n    # ... other middleware\n    'django_user_agents.middleware.UserAgentMiddleware',\n]\n\n# Optional: Recommended for performance\n# CACHES = {\n#     'default': {\n#         'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',\n#         'LOCATION': 'unique-snowflake',\n#     }\n# }\n# USER_AGENTS_CACHE = 'default'\n\n# views.py\nfrom django.shortcuts import render\nfrom django_user_agents.utils import get_user_agent\n\ndef my_view(request):\n    # Access via request.user_agent (if middleware is used)\n    is_mobile = request.user_agent.is_mobile\n    is_tablet = request.user_agent.is_tablet\n    is_pc = request.user_agent.is_pc\n    browser_family = request.user_agent.browser.family\n\n    # Alternatively, get it explicitly\n    # user_agent = get_user_agent(request)\n    # is_bot = user_agent.is_bot\n\n    context = {\n        'is_mobile': is_mobile,\n        'is_tablet': is_tablet,\n        'is_pc': is_pc,\n        'browser_family': browser_family,\n    }\n    return render(request, 'my_template.html', context)","lang":"python","description":"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)`."},"warnings":[{"fix":"Update your `settings.py` to use `MIDDLEWARE = [...]` and place `'django_user_agents.middleware.UserAgentMiddleware'` within that list.","message":"For 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.","severity":"breaking","affected_versions":"< 0.4.0 (for older Django), all versions (if migrating old settings)"},{"fix":"Ensure you run `pip install pyyaml ua-parser user-agents` before `pip install django-user-agents`.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Configure 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'`.","message":"Caching 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Complement 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.","message":"User-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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Thoroughly 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.","message":"The `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.","severity":"deprecated","affected_versions":"0.4.0 (with Python 3.8+ or Django 3.0+)"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}