[Zope] Accessing Zope REQUEST data from a Python module

Ken Winter ken at sunward.org
Wed Dec 5 19:35:22 EST 2007

I'm customizing a product.  To do what I'm trying to do, I need to be able
to access REQUEST data from within a Python module (not a script; a .py file
containing class and method definitions).

>From within a script, a line like

	req = context.REQUEST

does the job just fine.  But when I put that line into my product module, I
get a message like:

	Name "context" not defined

and I don't see any way to import that into my module.

At (http://www.plope.com/Books/2_7Edition/AppendixB.stx#0-8), the Zope API
Reference describes the module "HTTPRequest", which includes the class that
it calls "HTTPRequest(BaseRequest)", which apparently is the equivalent of
REQUEST as described above.  I have put this line into my module:

	from ZPublisher.HTTPRequest import HTTPRequest

but now I don't know how to reference the request's contents.  I put in this

	req = HTTPRequest

and print "req" to a debug file, and it shows:


which I don't understand.  I have tried a half-dozen ways (suggested by the
Zope API Reference) to get an actual property of the current HTTPRequest,
but without success.  Examples:

	req['URL'] -> "TypeError: unsubscriptable object"
	req.get('URL') -> "TypeError: unbound method get() must be called
with HTTPRequest instance as first argument (got str instance
	req.keys() -> "TypeError: unbound method keys() must be called with
HTTPRequest instance as first argument (got nothing instead)"

The last two suggest that, while I have successfully imported the
HTTPRequest *class*, I haven't successfully gotten the current HTTPRequest
*instance*.  I don't know how to do that.

So, I need an answer to any one of these questions:

1. How can the Zope global name "context" be defined or imported into a
non-script Python module?

2. How can one get the current instance of the class "HTTPRequest" into a
non-script Python module?

3. If I'm looking in the wrong direction, what is the right way to make the
current REQUEST's data available in a non-script Python module?

~ Ken 

More information about the Zope mailing list