I already use device discovery ( http://djangosnippets.org/snippets/2228/ ) in my templates and try to get it to work in the views too, so I can redirect to the app store if the user comes from iPhone.
So, I already had:
import re def mobile(request): device = {} ua = request.META.get('HTTP_USER_AGENT', '').lower() if ua.find("iphone") > 0: device['iphone'] = "iphone" + re.search("iphone os (\d)", ua).groups(0)[0] if ua.find("ipad") > 0: device['ipad'] = "ipad" if ua.find("android") > 0: device['android'] = "android" + re.search("android (\d\.\d)", ua).groups(0)[0].translate(None, '.')
And then create a class in tools / middleware.py:
from tools.context_processor import mobile class detect_device(object): def process_request(self, request): device = mobile(request) request.device = device
The following MIDDLEWARE_CLASSES value has been added in settings.py settings:
'tools.middleware.detect_device'
And in views.py I created:
def get_link(request): if request.device.iphone: app_store_link = settings.APP_STORE_LINK return HttpResponseRedirect(app_store_link) else: return HttpResponseRedirect('/')
But I get an error message:
'dict' object has no attribute 'iphone'
Christoffer
source share