How does one access request headers in TG2??

14 views
Skip to first unread message

Uwe C. Schroeder

unread,
May 28, 2009, 11:18:07 PM5/28/09
to turbo...@googlegroups.com

The docs say to

from webob import Request

and then one can do stuff like

Request.user_agent

which should return a string.
But hey, the request isn't initialized, so looking at the code of webobj:

def __get__(self, obj, type=None):
if obj is None:
return self
if self.key not in obj.environ:
if self.default_factory:
val = obj.environ[self.key] = self.default_factory()
return val
else:
return self.default
return obj.environ[self.key]

I get the "obj is None" version as the result of Request.user_agent - it is a
environ_getter object but no string.

Ok, I'm not doing this in the controller, I'm doing this in helpers.py in the
lib directory. What I need is a method to test for the user agent which is
available in all templates (just like stdvars.py used to do in TG1).
Is helpers.py loaded statically and not per request? If so, how can I have a
method in every template and where do I initialize it (BTW: the TG2 docs
don't talk about this at all, at least not that I could find it)

Any pointers as usual greatly appreciated.

Uwe

Uwe C. Schroeder

unread,
May 28, 2009, 11:50:13 PM5/28/09
to turbo...@googlegroups.com
Never mind. I just figured out that
from tg import request

instead of

from webob import Request as request

yields the expected result.
(might be nice to have a little mention of that in the docs though)

Uwe
--
UC

--
Open Source Solutions 4U, LLC 1618 Kelly St
Phone: +1 707 568 3056 Santa Rosa, CA 95401
Cell: +1 650 302 2405 United States
Fax: +1 707 568 6416
Reply all
Reply to author
Forward
0 new messages