Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize Display for Django WebApp depending on Mobile Device vs Desktop [closed]

What is the best way to detect the device accessing the page, and use logic to change the display depending on the browser device type?

like image 844
shashank Avatar asked Jan 06 '16 07:01

shashank


1 Answers

finally i got the solution: i use django-mobile for this, change in settings.py as per requirement and made a middleware.py for this.

settings.py:(first follow `django-mobile`)

DESKTOP_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates'),)
MOBILE_TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, 'templates/mobile'),)

then make middleware.py

middleware.py
from django.conf import settings

class MobileTemplatesMiddleware(object):
    """Determines which set of templates to use for a mobile site"""

    def process_request(self, request):
        # sets are used here, you can use other logic if you have an older version of Python
        MOBILE_SUBDOMAINS = set(['m', 'mobile'])
        domain = set(request.META.get('HTTP_HOST', '').split('.'))
        if request.flavour=='mobile':
            settings.TEMPLATE_DIRS = settings.MOBILE_TEMPLATE_DIRS
        else:
            settings.TEMPLATE_DIRS = settings.DESKTOP_TEMPLATE_DIRS
like image 71
shashank Avatar answered Oct 19 '22 03:10

shashank