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

倾然丶 夕夏残阳落幕 提交于 2019-12-21 20:17:13

问题


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():
    ...


来源:https://stackoverflow.com/questions/8272523/whats-a-good-user-agent-parsing-plugin-for-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!