how to pass a large list/array from a page to a new url

54 views
Skip to first unread message

Thang Nguyen

unread,
Oct 5, 2012, 8:42:45 PM10/5/12
to turbo...@googlegroups.com
Hi,

For a small number of parameters I can pass them to a new url via
query string such as

url:'${tg.url("/new_url")}?param1=${param1}&param2=${param2}'

However, what would be the correct method for passing 30 or more of
these parameters? For example, these parameters could be the
selected rows (and its attributes) from a large table.

thanks

TPN

Mengu

unread,
Oct 6, 2012, 3:42:02 AM10/6/12
to TurboGears
hi,

afaik url method takes two parameters. one for the url and one for the
query string. so you can pass all your parameters in a dict.

>>> url("an/action/", dict(a=1, b=2, c=3))
'an/action/?a=1&c=3&b=2'

Craig Small

unread,
Oct 10, 2012, 5:41:26 PM10/10/12
to turbo...@googlegroups.com
On Sat, Oct 06, 2012 at 12:42:02AM -0700, Mengu wrote:
> afaik url method takes two parameters. one for the url and one for the
> query string. so you can pass all your parameters in a dict.

Is there also the "other end" function somewhere in turbogears?
Pretty much every page I have uses some common options, for example
start/stop times.

Rather than having to write this for every page is there a place I can
put it once and every widget on every page knows its been sanity checked
(e.g. stop times are "later" than start times)

I've done this in php before by including some $_POST parser thing in
the second line of every file, but was hoping for a more elgant way.

- Craig
--
Craig Small VK2XLZ http://enc.com.au/ csmall at : enc.com.au
Debian GNU/Linux http://www.debian.org/ csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5

Alessandro Molina

unread,
Oct 11, 2012, 5:48:38 PM10/11/12
to turbo...@googlegroups.com
On Wed, Oct 10, 2012 at 11:41 PM, Craig Small <csm...@enc.com.au> wrote:
> On Sat, Oct 06, 2012 at 12:42:02AM -0700, Mengu wrote:
>> afaik url method takes two parameters. one for the url and one for the
>> query string. so you can pass all your parameters in a dict.
>
> Is there also the "other end" function somewhere in turbogears?
> Pretty much every page I have uses some common options, for example
> start/stop times.
>
> Rather than having to write this for every page is there a place I can
> put it once and every widget on every page knows its been sanity checked
> (e.g. stop times are "later" than start times)
>

There are various ways to achieve this.
Which one to use depends on various conditions :)

1) You can register a before_call/before_render/before_validate hook
in config/app_cfg with app_config.register_hook
2) You can add your check into BaseController.__call__ inside
lib/base.py, which gets called on each request.
3) You can subclass BaseController, add a _before method and let all
your controllers descend from it. _before will be called before
calling any exposed method of the subclass.

I would suggest going for the third as instead of being called always,
gets called only for controllers that inherit from your newly created
controller, making it both easy to enable or disable the behavior for
each controller.

ozwyzard

unread,
Oct 11, 2012, 11:26:39 PM10/11/12
to turbo...@googlegroups.com
Just a thought... 

What if you were to pack it, encrypt the parameters using a key (and pycrypto package), then encode it into a string (e.g. hex string) and pass it as 'one' parameter.  For an incoming parameter you would then decode it first and decrypt and unpack.  This will also prevent exposing DB fields to the world.

Michael Pedersen

unread,
Oct 14, 2012, 10:28:59 PM10/14/12
to turbo...@googlegroups.com
On Thu, Oct 11, 2012 at 11:26 PM, ozwyzard <ozwy...@gmail.com> wrote:
> What if you were to pack it, encrypt the parameters using a key (and
> pycrypto package), then encode it into a string (e.g. hex string) and pass
> it as 'one' parameter. For an incoming parameter you would then decode it
> first and decrypt and unpack. This will also prevent exposing DB fields to
> the world.

Depending on the size of the array being passed around, that could be
a bad idea. After all, it has to be downloaded by the client, then
uploaded back to the server. In fact, I disagree with that unless it's
absolutely necessary.

As for the original question, I would probably do something like this:

<a href="${tg.url('/new_url?')}${"&".join([lambda x: "%s=%s" % (x,
params[x]) for x in params])}">Link Text Here</a>

And make sure that params is a dict with all the parameters I want to
pass around.
--
Michael J. Pedersen
My Online Resume: http://www.icelus.org/ -- Google+ http://plus.ly/pedersen
Google Talk: m.ped...@icelus.org -- Twitter: pedersentg

Craig Small

unread,
Oct 16, 2012, 7:52:54 AM10/16/12
to turbo...@googlegroups.com
On Sun, Oct 14, 2012 at 10:28:59PM -0400, Michael Pedersen wrote:
> Depending on the size of the array being passed around, that could be
> a bad idea. After all, it has to be downloaded by the client, then
> uploaded back to the server. In fact, I disagree with that unless it's
> absolutely necessary.
Why not punt all the variables into the session and use session
variables. You only pass the session cookie then.

Not exactly sure how you set and get them, possibly fiddling with
tg.session
Reply all
Reply to author
Forward
0 new messages