Balaraju M
Sep 8, 2018 · 1 min read

Django-Agents For Knowing client details

A django package for easy identification of client’s details like platform[mobile, pc, tablet..],browser details and history, geolocation.Under the hood it uses python user-agents .

Installation

To install django-agents latest version, clone it from git, before doing that make sure you have installed user-agents python package on your machine.

step1:git clone https://github.com/balaraju1278/django-agents.git
step2: pip install — user django-agents/dist/django-agents-0.1.tar.gz

if you wanna uninstall this package use pip, pip uninstall django-agents

Configuration:
add the following lines of snippet of code in you settings.py file
INSTALLED_APPS = [
# django apps,
‘django-agents’,
]
MIDDLEWARE_CLASSES = [
‘django-agents.middlewaere.DjangoUserAgentMiddleware’,
]

thats all, all your work is done.

How to Get use from it
By adding django-agents.middlewaere.DjangoUserAgentMiddleware to middleware class user-agent attribute will now added to request.which we can use in views.py

def my_dajngo_view(request):
if request.user_agent.is_mobile:
# do stuff here
if request.user_agent.is_tablet:
# do stuff here
if request.user_agent.is_pc:
# do stuff here

def my_browser_view(request):
# fetch user_details:
request.user_agent.browser
request.user_agent.browser.family
request.user_agent.browser.version

def my_os_view(request):
# fetch os details
request.user_agent.os
request.user_agent.os.family
request.user_agent.os.version

from django-agents.utils import get_django_user_agent

def my_user_agent(request):
ua = get_django_user_agent(request)
if ua.is_mobile:
# busines logic here

How to use it in template
add the following line of code on top of your template
{% load django_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 %}

Balaraju M

Written by

Dango lover, Full stack developer , Startups and products