What's a good User-Agent parsing plugin for dj

2020-07-18 12:11发布

问题:

Is there a User-Agent-String parsing plugin or middleware that you can recommend. Right now I am using django-smartagent, but its dictionary is not complete and not updated very often.

回答1:

The browscap files can be read by the pythong ConfigParser library and are updated regularly.

http://browsers.garykeith.com/downloads.asp

This is a module that shows using ConfigParser to read, from a older djangosnippet posting (267) slightly modified version to allow for easier 'finding' of the browscap.ini file

http://pastebin.com/viD5F7nV

Then in middleware

BCAP=browsecap.BrowserCapabilitues()

class UAMiddleware:
    def process_request(self,request):
        request.browser=BCAP(request.META.get('HTTP_USER_AGENT',''))

and in view..

if request.browser.is_mobile():
    ...