如何在 Django 中检测浏览器类型?

如何检测客户端使用的浏览器类型。我有一个问题,我不得不要求人们使用不同的浏览器(Firefox)而不是 IE。我怎样才能得到这些信息。

我知道 http 请求有这个信息(头)。在 Django 框架中,如何从 view.py 获取导航器.appName?

55725 次浏览

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

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'

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'])