find out about browser type

3 views
Skip to first unread message

steph

unread,
May 13, 2008, 8:45:48 AM5/13/08
to TurboGears
hi group,

maybe this is a very basic question, so please be patient. how can I
find out about the browser that is accessing a page rendered by
turbogears? I want to steer my layout based on this information.

Thanks,
stephan

Christoph Zwerschke

unread,
May 13, 2008, 10:35:23 AM5/13/08
to turbo...@googlegroups.com
steph schrieb:

> maybe this is a very basic question, so please be patient. how can I
> find out about the browser that is accessing a page rendered by
> turbogears? I want to steer my layout based on this information.

You can use tg.useragent for that purpose:
http://docs.turbogears.org/1.0/stdvars#standard-variables

-- Christoph

steph

unread,
May 13, 2008, 3:24:17 PM5/13/08
to TurboGears
Thanks - but apparently just for what I aim at it does not help: I
wanted to check for the browser type AND version, for instance IE6.
Can I do that somehow?

br,
Stephan

Uwe C. Schroeder

unread,
May 13, 2008, 3:31:44 PM5/13/08
to turbo...@googlegroups.com, steph

On Tuesday 13 May 2008, steph wrote:
> Thanks - but apparently just for what I aim at it does not help: I
> wanted to check for the browser type AND version, for instance IE6.
> Can I do that somehow?

sure you can. Something like this would detect IE 6 (very simply speaking)

from cherrypy import request

def isIE6():
if request.headers.get('user-agent','').find('MSIE 6') > 0:
return True
return False

and if you want to use it in templates, put this into your stdvars.py:

from wherever-it-is import isIE6

def add_custom_stdvars(vars):
vars.update({"isIE6":isIE6})
return vars

turbogears.view.variable_providers.append(add_custom_stdvars)


then you can use it as <div py:if="tg.isIE6()"> ... </div>

HTH
Uwe


steph

unread,
May 23, 2008, 11:03:44 AM5/23/08
to TurboGears
Thank you, this solved my problem :-)
Reply all
Reply to author
Forward
0 new messages