32👍
Based on your prior use of mobile detection middleware, I’d recommend the following:
Pick up the Python port of MobileESP (source code here) and drop it into a folder named mobileesp
in the base of your project (where manage.py
is). Throw in a blank __init__.py
file so that Python will see it as a package.
Go ahead and create a new file, middleware.py
, in that directory, and fill it with:
import re
from mobileesp import mdetect
class MobileDetectionMiddleware(object):
"""
Useful middleware to detect if the user is
on a mobile device.
"""
def process_request(self, request):
is_mobile = False
is_tablet = False
is_phone = False
user_agent = request.META.get("HTTP_USER_AGENT")
http_accept = request.META.get("HTTP_ACCEPT")
if user_agent and http_accept:
agent = mdetect.UAgentInfo(userAgent=user_agent, httpAccept=http_accept)
is_tablet = agent.detectTierTablet()
is_phone = agent.detectTierIphone()
is_mobile = is_tablet or is_phone or agent.detectMobileQuick()
request.is_mobile = is_mobile
request.is_tablet = is_tablet
request.is_phone = is_phone
Lastly, make sure to include 'mobileesp.middleware.MobileDetectionMiddleware',
in MIDDLEWARE_CLASSES
in your settings file.
With that in place, in your views (or anywhere that you have a request object) you can check for is_phone
(for any modern smartphones), is_tablet
(for modern tablets) or is_mobile
(for any mobile devices whatsoever).
5👍
Have a look at MobileESP. It has been recently ported to Python for Django web app framework. It can detect various classes and tiers of devices (including smatphones, tablets).
- [Django]-How to delete project in django
- [Django]-Getting Values of QuerySet in Django
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
1👍
If you want some quick and simple solution, you can try handset detection’s javascript that enables you create simple redirection rules.
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Django Generic Views using decorator login_required
1👍
from django_user_agents.utils import get_user_agent
def my_view(request):
user_agent = get_user_agent(request)
if user_agent.is_mobile:
# identified as a mobile phone (iPhone, Android phones, Blackberry, Windows Phone devices etc)
if user_agent.is_tablet:
# identified as a tablet device (iPad, Kindle Fire, Nexus 7 etc)
if user_agent.is_pc:
# identified to be running a traditional "desktop" OS (Windows, OS X, Linux)
It uses user_agents
under the hood.
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-How to pass django rest framework response to html?
- [Django]-Django Admin app or roll my own?
0👍
I was looking for something like this and I stumbled upon django-mobile which does exactly that.
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Using the reserved word "class" as field name in Django and Django REST Framework
- [Django]-Using django-admin on windows powershell