88

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 ?

AlgoMan
  • 2,785
  • 6
  • 34
  • 40

5 Answers5

141

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

request.META['HTTP_USER_AGENT']
digitaldreamer
  • 52,552
  • 5
  • 33
  • 28
  • But with java script i can get the browser information by just doing document.write("Browser Name: " + navigator.appName); which gives Microsoft Internet Explorer . There is no direct method of getting it ? I mean i have to parse the user agent and search for MSIE . – AlgoMan Apr 19 '10 at 17:10
  • 3
    In the past I have written custom middleware to handle browser detection for stuff like mobile. If you go this rout you really need to be careful with your caching or else things will randomly fail in strange ways. If you only need to make exceptions for IE it's best to use the IE Conditional Comments http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx . – digitaldreamer Apr 19 '10 at 18:46
  • I think conditional comments are simpler :). Thanks digitaldreamer – AlgoMan Apr 19 '10 at 20:23
48

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'
Mark
  • 18,730
  • 7
  • 107
  • 130
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
  • 1
    how do I get the IP Address of the connecting device? – Nie Selam Jan 16 '19 at 20:52
  • Two other User Agent parser for python are https://pypi.org/project/httpagentparser/ and https://github.com/thinkwelltwd/device_detector both of which doesn't need to be added to middleware. – Shan Eapen Koshy Jan 05 '21 at 04:38
11

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

Alex Sexton
  • 10,401
  • 2
  • 29
  • 41
  • 9
    how can we parse it out? Here is the string, and browser info is all over the place. Got the point? `Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36` – A.J. Mar 13 '14 at 09:57
1

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...

Community
  • 1
  • 1
J0ANMM
  • 7,849
  • 10
  • 56
  • 90
1

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'])
Cubiczx
  • 1,005
  • 11
  • 10