[ZPT] Passing parameter to Python Script

Tino Wildenhain tino@wildenhain.de
Fri, 19 Apr 2002 15:27:16 +0200


Hi Grant,



--On Thursday, April 18, 2002 11:42:17 -0700 Grant Thibdeau 
<miket@davisvision.com> wrote:

> Hello Folks, this is another simple question that I seem to be having
> issues with, i'm attempting to get a value from my url, it's appended to
> the end like --
>
> http://www.link.com/PageTemplate?rebate=100
>
> So that goes to my PageTemplate, which has several macros in it, one of
> the macros calls a python script which does some stuff with the rebate
> number in the url. How do I pass the value from the url to the script
> call in the zpt? This is what my script call looks like --
>
>
>        <p tal:content="root/Lens123/Python/pyGetRebate"
>
> but whenever I try to add anything to the call, it starts looking for the
> literal thing.  So I tried to do a python statement like so --
>
>         <p tal:content="python: container.pyGetRebate()"
>
> which gives me a TALES error, a couple other iterations of it do not work
> either, such as dropping the parentheses and rebate is a parameter on the
> script, would I need other parameters? Any help or suggestions would be
> greatly appreciated, or if this isn't the correct idea behind using this
> please let me know.  In my script i'm trying to assign the rebate number
> like so --
>
> argRebateNumber=form['rebate']
>
You are close :-)

If you look at the binding page and the documentation >;^> for
python scripts you will discover that along the explicit passed
arguments to a python script there are some magically always present
variables available. The most prominent ones are context, container
context gives you the calling context, which holds amongst others
the REQUEST subobject. This REQUEST subobject holds all information
the ZPublisher gets from the browser and subsequent work with the data.
One subobject of REQUEST is form, which is a dictionary alike object
for the form data (both GET -> URL and POST data)
So if your template gets called like you mentionmed above,
context.REQUEST.form.get('rebate') will give you your value of '100'
(Yes its a string, unless you define the name of the variable with :int)
Since the sample script already starts with something like that:

request=context.REQUEST

you can access your value like this:


request.form['rebate']
request.form.get('rebate')

not that both forms will raise an exception if the value does not exist.
So you probably want to use:
request.form.get('rebate',default)  with a reasonable default value.

Happy developing
Tino