Documentation:
https://pypi.org/project/django-user-agents/
User-Agent strings are part of the HTTP request headers. While they can be easily spoofed or modified, for the vast majority of everyday Internet users they provide reliable information about the client, such as:
- Browser
- Operating System
- Device type (mobile, tablet, desktop)
The django-user-agents module makes it simple to parse and structure this information, so you can use it in your application without dealing with raw User-Agent strings directly.
pip install django-user-agents
INSTALLED_APPS = [
...
'django_user_agents',
]
MIDDLEWARE = [
...
'django_user_agents.middleware.UserAgentMiddleware',
]
from django.views.generic import TemplateView
class index(TemplateView):
template_name = "polls/index.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
ua = self.request.user_agent
user_agent_info = {
"is_mobile": ua.is_mobile,
"is_tablet": ua.is_tablet,
"is_touch_capable": ua.is_touch_capable,
"is_pc": ua.is_pc,
"is_bot": ua.is_bot,
"browser_family": ua.browser.family, # ex: 'Chrome'
"browser_version": ua.browser.version, # ex: ('117', '0', '5938')
"browser_string": str(ua.browser), # ex: 'Chrome 117.0.5938'
"os_family": ua.os.family, # ex: 'iOS'
"os_version": ua.os.version, # ex: ('17', '0')
"os_string": str(ua.os), # ex: 'iOS 17.0'
"device_family": ua.device.family, # ex: 'iPhone'
"device_brand": ua.device.brand, # ex: 'Apple'
"device_model": ua.device.model, # ex: 'iPhone'
"user_agent_string": self.request.META.get("HTTP_USER_AGENT", ""),
}
context["user_agent_info"] = user_agent_info
return context
<ul>
{% for key, value in user_agent_info.items %}
<li>
<span style="color:rgb(249, 250, 215)">{{ key }}:</span>
<span style="color:lime;">{{ value }}</span>
</li>
{% endfor %}
</ul>
Django 5.2 django-user-agents==0.4.0
02 Oct. 2025
|
Last Updated: 03 Dec. 2025
|
jaimedcsilva Related