开发者

Django elegant rendering engine redirect quest

I'm trying to find a nice and elegant solution for detecting rendering engines (mozilla, webkit, etc) and redirecting visitors to the right templates and css.

Currently I have this setup:

detection(middleware):

import re

class BrowserDetectionMiddleware(object):

    def __init__(self):
        self.data_browser = [
            {
                'renderingEngine': "WebKit",
                'subString': "Chrome",
                'identity': "Chrome",
                'versionSearch': "Chrome/"
            },
            {
                'renderingEngine': "WebKit",
                'subString': "Apple",
                'identity': "Safari",
                'versionSearch': "Version/"
            },
            {
                'renderingEngine': "None",
                'subString': "Opera",
                'identity': "Opera",
                'versionSearch': "Version/"
            },
            {
                'renderingEngine': "Mozilla",
                'subString': "Firefox",
                'identity': "Firefox",
                'versionSearch': "Firefox/"
            },
            {
                'renderingEngine': "MSIE",
                'subString': "MSIE",
                'identity': "Explorer",
                'versionSearch': "MSIE "
            },
        ]

    def process_request(self, request):
        request.browser_name = None
        request.browser_version = None
        request.browser_renderingEngine = None
        for b in self.data_browser:
            if b['renderingEngine'] in request.META['HTTP_USER_AGENT']:
                request.browser_renderingEngine = b['renderingEngine']
            if b['subString'] in request.META['HTTP_USER_AGENT']:
                request.browser_name = b['identity']
                if b.has_key('versionSearch'):
                    f = re.compile(b['versionSearch']+'([0-9\.]+)',re.M)
                    m = f.search(request.META['HTTP_USER_AGENT'])
                开发者_开发知识库    if m:                
                        request.browser_version = m.group(1)
                break

    def process_view(self, request, view_func, view_args, view_kwargs):
        pass

    def process_response(self, request, response):
        return response

    def process_exception(self, request, exception):
        pass

view:

from django.http import Http404
from django.shortcuts import render_to_response


def rendering_engine_dispatcher(request):

    PREFIX = request.browser_renderingEngine

    if not PREFIX is None:
        str.lower(PREFIX)
        return render_to_response(PREFIX+'/home.xhtml', {})
    else:
        raise Http404

This works fine, but I rather regulate everything via Middleware.. My other idea is to create a render_to_response_prefixed function..?

I would love some elegant solution directions.. :-)


The most elegant solution is to use a cross-browser CSS framework (like blueprint, 960.gs, 52framework, etc). There are a ton out there, pick one...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜