I need to change what template to use in a response based on type of
user-agent in request. The middleware below works ok to detect mobile
client, but I cannot get the template manipulation in the process_view
method  to work.

The middleware:

# Credits: http://djangosnippets.org/snippets/2001/
import re


class MobileDetectionMiddleware(object):
    """
    Useful middleware to detect if the user is
    on a mobile device.
    """

    def process_view(self, request, view_func, view_args, view_kwargs):
        if not request.is_mobile:
            return

        print vars(view_func), view_args,view_kwargs # these are
allways blank/empty
        template = view_kwargs.get("template")
        if template is None and view_func.func_defaults:
            for default in view_func.func_defaults:
                if str(default).endswith(".html"):
                    template = default
                    break

        if template is not None:
            template = template.rsplit(".html", 1)[0] + ".mobile.html"
            try:
                get_template(template)
            except TemplateDoesNotExist:
                return
            else:
                view_kwargs["template"] = template

        return view_func(request, *view_args, **view_kwargs)

    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.
            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

        request.is_mobile = is_mobile


The view:


from django.shortcuts import *

def index(request):
    return render_to_response('testapp/test.html', {'user':request.user})



Any clues?


-- 
Mvh/Best regards,
Thomas Weholt
http://www.weholt.org

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to