Context object list and controllers + html

0 views
Skip to first unread message

gazza

unread,
Nov 9, 2009, 10:07:05 AM11/9/09
to pylons-discuss
Hello,

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 though I could embed a hidden like:

<INPUT TYPE=hidden NAME=listitems VALUE=c.chosenList>

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.

Thanks,
gazza

Jonathan Vanasco

unread,
Nov 9, 2009, 12:46:22 PM11/9/09
to pylons-discuss

<INPUT TYPE="hidden" NAME="listitems" VALUE="${c.chosenList"} />

gazza

unread,
Nov 9, 2009, 5:47:27 PM11/9/09
to pylons-discuss

Unfortunately I'm not seeing what I expect to see. I want to retrieve
the list of objects

from the c.chosenList:

say the list is<2,4>

<INPUT TYPE="hidden" NAME="listitems" VALUE="${c.chosenList}" />

Within testController:

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

Matt Feifarek

unread,
Nov 9, 2009, 6:03:15 PM11/9/09
to pylons-...@googlegroups.com
On Mon, Nov 9, 2009 at 4:47 PM, gazza <bursl...@yahoo.com> wrote:

from the c.chosenList:

say the list is<2,4>

<INPUT TYPE="hidden" NAME="listitems" VALUE="${c.chosenList}" />

Within testController:

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
]

When you put ${c.chosenList} into your output template, it's no longer python; it's part of your html string; your html page would look like this:

<INPUT TYPE="hidden" NAME="listitems" VALUE="[2, 4]" />

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?

Jonathan Vanasco

unread,
Nov 9, 2009, 7:01:18 PM11/9/09
to pylons-discuss
sorry for the confusion above.

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.

gazza

unread,
Nov 9, 2009, 8:22:42 PM11/9/09
to pylons-discuss
Much appreciated the responses.
% for i in c.listitems:
> <INPUT TYPE="hidden" NAME="listitems" VALUE="${i} " />
> % endfor

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
Reply all
Reply to author
Forward
0 new messages