I am passing a context object which is a list of objects to a html
page. Within the page I then
cycle through the objects and display them. Pretty trivial. However I
want to pass the list of objects back to the controller so I can
continue to manipulate the objects.
I am having problems doing this and unsure if this is the correct
approach? Clarification and enlightenment is needed on this and much
appreciate the help.
def test(self,id):
locallist = request.params.get("listitems")
for index,item in enumerate(locallist):
print "%s",item
I was expecting it to obtain pass the original list and index through
through it. Instead I see the below
[
2
,
4
]
Its look each item is a single character?
How do you pass a context object containing a list of items to an html
page and preserve the list of objects within the context object to be
then
passed back to the controller? Is it possible?
I apologise in advance on any confusion on this.
Thanks,
Garyc
On Nov 9, 11:46 am, Jonathan Vanasco <jonat...@findmeon.com> wrote:
When that is posted back to the server, it simply comes in as a string: "[2, 4]". If you want to "parse" that string into a python object, you'll have to do that in your controller, or perhaps with FormEncode.
If you're trying to send multiple values in your form, you'll need multiple form elements... like a checkbox set. Those kinds of form elements can send multiple values, and that will be reflected in request.params.
The key concept is that HTML doesn't understand anything about Python variables... how could it?
The above was the approach I thought about but this looks a wee' bit
verbose creating multiple instances
. Instead I think I will limit the passing of context objects to be no
more than a single element and push the list into
a DB which it will eventually end up.
Thanks,
Gazza
On Nov 9, 6:01 pm, Jonathan Vanasco <jonat...@findmeon.com> wrote:
> Anything put into the "c" variable in a controller is accessible
> throughout the request by Controllers and Views(templates)
> ${} is shorthand for "print the result of the brackets
> Assuming that c.listitems is an array of list item ids, in your
> template you can do:
> <INPUT TYPE="hidden" NAME="listitems" VALUE="${ ','.join
> (c.listitems) } " />
> or
> % for i in c.listitems:
> <INPUT TYPE="hidden" NAME="listitems" VALUE="${i} " />
> % endfor
> if c.listitems is an array of objects, you might do:
> % for i in c.listitems:
> <INPUT TYPE="hidden" NAME="listitems" VALUE="${i.id} " />
> % endfor
> You should read through the tutorial and possibly the Mako templating
> docs themselves. All of this information is explained in there.