开发者

How to detect Browser type in Django?

How can i detect which browser type the client is using. 开发者_开发技巧I have a problem where i have to ask people to use different browser (Firefox) instead of IE. How can i get this information.

I know http request has this information (Header). How will i get the navigator.appName from the view.py in the Django framework ?


You can extract that information from the request object like so:

request.META['HTTP_USER_AGENT']


There are multiple ways of getting that done.

The easiest way is what @digitaldreamer recommended. That is you can make a meta request for HTTP_USER_AGENT.

request.META['HTTP_USER_AGENT']

But I would also recommend you to take a look at the Django User Agents library.

Install it with pip

pip install pyyaml ua-parser user-agents
pip install django-user-agents

And configure settings.py:

MIDDLEWARE_CLASSES = (
    # other middlewares...
    'django_user_agents.middleware.UserAgentMiddleware',
)

INSTALLED_APPS = (
    # Other apps...
    'django_user_agents',
)

# Cache backend is optional, but recommended to speed up user agent parsing
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}

# Name of cache backend to cache user agents. If it not specified default
# cache alias will be used. Set to `None` to disable caching.
USER_AGENTS_CACHE = 'default'

Usage is pretty simple as well.

A user_agent attribute will now be added to request, which you can use in views.py:

def my_view(request):

# Let's assume that the visitor uses an iPhone...
request.user_agent.is_mobile # returns True
request.user_agent.is_tablet # returns False
request.user_agent.is_touch_capable # returns True
request.user_agent.is_pc # returns False
request.user_agent.is_bot # returns False

# Accessing user agent's browser attributes
request.user_agent.browser  # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
request.user_agent.browser.family  # returns 'Mobile Safari'
request.user_agent.browser.version  # returns (5, 1)
request.user_agent.browser.version_string   # returns '5.1'

# Operating System properties
request.user_agent.os  # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
request.user_agent.os.family  # returns 'iOS'
request.user_agent.os.version  # returns (5, 1)
request.user_agent.os.version_string  # returns '5.1'

# Device properties
request.user_agent.device  # returns Device(family='iPhone')
request.user_agent.device.family  # returns 'iPhone'


You can look into the 'user agent string' and parse out the values.

Here's the relevant docs, specifically on (HTTP_USER_AGENT):

http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META


From this SO question, a pure html solution using conditional comments:

<!--[if IE]> 
    <div>
       This site is not rendered properly with Internet Explorer. 
       Please use Firefox instead
    </div>
<![endif]-->

As warned by the documentation:

As of Internet Explorer 10, conditional comments are no longer supported by standards mode. Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser

I tested it in IE7, IE9, IE10 and IE11. The only version where this did not work was IE10...


To detect if is internet explorer 8 or older IE:

is_IE_8_or_lower = re.findall(r'MSIE [2-8]',request.request.META['HTTP_USER_AGENT'])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜