[Zope] modifying a url's query string
Tino Wildenhain
tino@wildenhain.de
Wed, 26 Mar 2003 18:51:58 +0100
Hi Mark,
Roach, Mark R. wrote:
> Hi, I'm wondering if there is a good way to modify the query string of a
> get request to change a single variable. For example, let's say my dtml
> method is accessed like this:
>
> http://myzope/MyFolder/method?var1=foo&var2=bar
>
> now I want one of the links on the page to load the same page, only with
> a different value for var2.
>
> I could use something like
> <dtml-var absolute_url>?var1=&dtml-var1;&var2=newvalue
>
> but that seems cumbersome the longer the list of variables gets.
>
...
>
> full_url_with_query({'var1':'newvalue'})
This is ZTUtils.make_query().
>
> but it seems that the only source of variable info is the (quite large)
> REQUEST object. Is there any way to pull only the query string variables
yes, this is the subobject "form" of REQUEST.
> out of the request, or even better, a function that does what my
> imaginary "full_url_with_query" function wants to do?
Ok, some examples, requiring you do it inside a python script "Script
(Python)" as you would do anyway for logic:
# simple copy:
from ZTUtils import make_query
form = container.REQUEST.form
return apply(make_query,(),form)
# copy with replacement:
from ZTUtils import make_query
form = container.REQUEST.form
form.update({'value1':'somevalue',
'value2':'othervalue'})
return apply(make_query,(),form)
# copy with defaults:
from ZTUtils import make_query
form = container.REQUEST.form
defauls = {'value1':'somevalue',
'value2':'othervalue'}
defaults.update(form)
return apply(make_query,(),form)
# the most simple variable only:
from ZTUtils import make_query
return make_query(value1=1234,value2='foobar',value3=[1,2,3])
# this also shows how different types are handled.
HTH
Tino