Ulmer
Ulmer

Reputation: 1580

How do I check if a User is using a mobile device in Django 1.9?

I'm using Django 1.9 with Python 2.7 and I'm trying to get my app to recognize if the user is browsing with a mobile device. I've tried django_mobile but it seems outdated for django 1.9 because there aren't even template_loaders to install the app, am I wrong about this?

Upvotes: 3

Views: 5717

Answers (4)

prokaktus
prokaktus

Reputation: 632

Yes, you are little wrong. To install django_mobile with Django 1.9 you should update settings (I've described this in the following PR, not merged yet). It works fine for me.

Exactly, you should replace TEMPLATE_LOADERS with loaders and TEMPLATE_CONTEXT_PROCESSORS with context_processors in TEMPLATES dictionary. For more about template options, read the docs.

Upvotes: 0

interDist
interDist

Reputation: 557

The MobileESP library may help in this case. It is not Django-specific, but can be used with Django as a Python module. The API page details the detection capabilities, such as tiers (tablet / touchscreen smartphone) and specific platforms.

Upvotes: 0

doniyor
doniyor

Reputation: 37856

or you can use django-user_agents app. really good one. you also get the context in your template - among others important for rendering some ads depending on device

in view

request.user_agent.is_mobile

or in template

{% if request.user_agent.is_mobile %}
    Do stuff here...
{% endif %}

Upvotes: 6

James Evans
James Evans

Reputation: 850

Try extracting the user agent string with

request.META['HTTP_USER_AGENT']

and then using this library to parse that string.

Example

from user_agents import parse

ua_string = request.META['HTTP_USER_AGENT']
user_agent = parse(ua_string)
if user_agent.is_mobile:
   ...

Upvotes: 0

Related Questions