为什么我的重定向中间件失败了?

时间:2012-11-13 18:53:09

标签: django mobile django-middleware

我有一个将移动用户重定向到移动网站的中间件,但如果网址为/property/,我想将其引导至完整网站。移动重定向正在运行,但/property/未被排除。

这是当前的中间件。

middleware.py

# Adapted from http://djangosnippets.org/snippets/2001/
import re
from  django.conf import settings
from django.http import HttpResponseRedirect


class MobileRedirectMiddleware(object):
    """
    Redirects mobile users to a different site.
    """
    def process_request(self, request):
        if self._is_mobile(request):
            return HttpResponseRedirect(settings.MOBILE_SITE_URL)

    def _is_mobile(self, request):
        is_mobile = False
        NON_MOBILE_REDIRECT_URLS = getattr(settings, 'NON_MOBILE_REDIRECT_URLS', [])
        if request.path in NON_MOBILE_REDIRECT_URLS:
            return False

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

            # Test common mobile values.
            pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)"
            prog = re.compile(pattern, re.IGNORECASE)
            match = prog.search(user_agent)

            if match:
                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']

                    pattern = "application/vnd\.wap\.xhtml\+xml"
                    prog = re.compile(pattern, re.IGNORECASE)

                    match = prog.search(http_accept)

                    if match:
                        is_mobile = True

            if not is_mobile:
                # Now we test the user_agent from a big list.
                user_agents_test = ("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", "winw",
                                    "xda-",)

                test = user_agent[0:4].lower()
                if test in user_agents_test:
                    is_mobile = True

        return is_mobile
settings.py中的

我有:

MOBILE_SITE_URL = 'http://mobile.somesite.com/'
NON_MOBILE_REDIRECT_URLS = ['/property/']

1 个答案:

答案 0 :(得分:1)

仅仅避免在给定网址处重定向到mobile.可能是不够的。如果用户已来自mobile.somesite.com/..../,您需要主动将其重定向到www.以远离移动网站。

这是未经测试的,但应该非常接近:

class MobileRedirectMiddleware(object):
    """
    Redirects mobile users to a different site.
    """
    def process_request(self, request):
        was_mobile = settings.MOBILE_SITE_URL in request.META.HTTP_REFERER
        NON_MOBILE_REDIRECT_URLS = getattr(settings, 'NON_MOBILE_REDIRECT_URLS', [])

        if request.path in NON_MOBILE_REDIRECT_URLS and was_mobile:
            # redirect them to 'www.somesite.com/.../'
            return HttpResponseRedirect(settings.MAIN_SITE_URL + request.path.lstrip('/'))

        if self._is_mobile(request):
            return HttpResponseRedirect(settings.MOBILE_SITE_URL)

    def _is_mobile(self, request):
        is_mobile = False

        # no longer need to check urls in here

        if request.META.has_key('HTTP_USER_AGENT'):
             ...