2

我刚刚试了一下django-moble

我真的很喜欢这个概念,并帮助我的项目检测移动设备。但是,我试图找出 iPad 浏览器何时发出请求。我已将以下内容添加到 Settings.py FLAVORS = ('full', 'mobile','ipad')

但它不起作用。有人可以告诉我如何从这里开始吗?我还需要做什么?

以下是我的看法。

if get_flavour()=='full':
  t = loader.get_template('index.html')
elif get_flavour()=='ipad':
  t = loader.get_template('ipad.html')
else:
  t = loader.get_template('mobile.html')

提前感谢您的时间。

4

2 回答 2

5

开箱即用,Django-mobile 仅提供两种风格。从github 页面

注意:默认情况下,django-mobile 只区分完整版和移动版。

为了实际检测 iPad 与任何其他设备,您需要将现有的MobileDetectionMiddleware替换MyMobileDetectionMiddleware为. 可以使用现有的类作为指导,在同一个github页面上有一些关于定制的信息MIDDLEWARE_CLASSESsettings.pyMobileDetectionMiddleware

于 2013-06-09T03:36:37.613 回答
0

如果您想要快速轻松地修复将 ipad 包含为“移动”风格,那么您可以通过注释掉(或删除)第42行的 user_agents_exception_search 来修改中间件类还要在第51行注释掉 self.user_agents_exception_search_regex 。然后在第60行,从“if 语句”中删除“and not”。

这是懒人的简单修改代码:

import re
from django_mobile import flavour_storage
from django_mobile import set_flavour, _init_flavour
from django_mobile.conf import settings


class SetFlavourMiddleware(object):
    def process_request(self, request):
        _init_flavour(request)

        if settings.FLAVOURS_GET_PARAMETER in request.GET:
            flavour = request.GET[settings.FLAVOURS_GET_PARAMETER]
            if flavour in settings.FLAVOURS:
                set_flavour(flavour, request, permanent=True)

    def process_response(self, request, response):
        flavour_storage.save(request, response)
        return response


class MobileDetectionMiddleware(object):
    user_agents_test_match = (
        "w3c ", "acs-", "alav", "alca", "amoi", "audi",
        "avan", "benq", "bird", "blac", "blaz", "brew",
        "cell", "cldc", "cmd-", "dang", "doco", "eric",
        "hipt", "inno", "ipaq", "java", "jigs", "kddi",
        "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
        "maui", "maxo", "midp", "mits", "mmef", "mobi",
        "mot-", "moto", "mwbp", "nec-", "newt", "noki",
        "xda",  "palm", "pana", "pant", "phil", "play",
        "port", "prox", "qwap", "sage", "sams", "sany",
        "sch-", "sec-", "send", "seri", "sgh-", "shar",
        "sie-", "siem", "smal", "smar", "sony", "sph-",
        "symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
        "upg1", "upsi", "vk-v", "voda", "wap-", "wapa",
        "wapi", "wapp", "wapr", "webc", "winw", "xda-",)
    user_agents_test_search = u"(?:%s)" % u'|'.join((
        'up.browser', 'up.link', 'mmp', 'symbian', 'smartphone', 'midp',
        'wap', 'phone', 'windows ce', 'pda', 'mobile', 'mini', 'palm',
        'netfront', 'opera mobi', 'ipad',
    ))
    #user_agents_exception_search = u"(?:%s)" % u'|'.join((
    #   'ipad',
    #))
    http_accept_regex = re.compile("application/vnd\.wap\.xhtml\+xml", re.IGNORECASE)

    def __init__(self):
        user_agents_test_match = r'^(?:%s)' % '|'.join(self.user_agents_test_match)
        self.user_agents_test_match_regex = re.compile(user_agents_test_match, re.IGNORECASE)
        self.user_agents_test_search_regex = re.compile(self.user_agents_test_search, re.IGNORECASE)
        #self.user_agents_exception_search_regex = re.compile(self.user_agents_exception_search, re.IGNORECASE)

    def process_request(self, request):
        is_mobile = False

        if request.META.has_key('HTTP_USER_AGENT'):
            user_agent = request.META['HTTP_USER_AGENT']

            # Test common mobile values.
            if self.user_agents_test_search_regex.search(user_agent):
                is_mobile = True
            else:
                # Nokia like test for WAP browsers.
                # http://www.developershome.com/wap/xhtmlmp/xhtml_mp_tutorial.asp?page=mimeTypesFileExtension

                if request.META.has_key('HTTP_ACCEPT'):
                    http_accept = request.META['HTTP_ACCEPT']
                    if self.http_accept_regex.search(http_accept):
                        is_mobile = True

            if not is_mobile:
                # Now we test the user_agent from a big list.
                if self.user_agents_test_match_regex.match(user_agent):
                    is_mobile = True

        if is_mobile:
            set_flavour(settings.DEFAULT_MOBILE_FLAVOUR, request)
        else:
            set_flavour(settings.FLAVOURS[0], request)
于 2013-11-20T13:45:42.380 回答