Toscawidgets form with multiple buttons

87 views
Skip to first unread message

Jamie Bullock

unread,
Aug 25, 2009, 10:40:53 AM8/25/09
to TurboGears

Hi,

I'm sure this is really simple, but I'm stumped right now.

Basically, I want a form with multiple buttons to be processed by a
single handler, but run sections of handler code conditionally
depending on which button was pressed.

class MyForm(TableForm):
class fields(WidgetsList):
fooButton = SubmitButton(attrs={'value':'foo'})
barButton = SubmitButton(attrs={'value':'bar'})
new_my_form = MyForm("new_my_form")

Then:

def post(self, *arg, **kw):

if kw['submit'] == 'foo':
do_something()
if kw['submit'] == 'bar':
do_something_else()

Is this possible?

Jamie





Crusty

unread,
Aug 26, 2009, 8:41:56 AM8/26/09
to turbo...@googlegroups.com
Hey Jamie,

you answered your own question, or what is the problem with that?
It's done almost exactly the way you wrote it, except for some ugly
pecularities of how TW sets the value attribute, which is a bit twisted.

Could you be more specific what goes wrong on your code?

Tom

Jamie Bullock

unread,
Aug 26, 2009, 1:16:20 PM8/26/09
to turbo...@googlegroups.com

Hi Tom,

Thanks for replying.

The problem is that kw['submit'] results in a KeyError exception.
Inspecting the kw dict shows there isn't element in the dict
corresponding to the submit button itself.

Any ideas?

Jamie

Crusty

unread,
Aug 27, 2009, 7:13:54 AM8/27/09
to turbo...@googlegroups.com
Hey Jamie,

you named your buttons:


 fooButton = SubmitButton(attrs={'value':'foo'})
 barButton = SubmitButton(attrs={'value':'bar'})

so the respective kw fields should be kw['fooButton'] and kw['barButton']
If these don't exist as well it might have the following reason

I am working more with Sprox than with TW, and sprox does _not_ give SubmitButtons a name (<input type="submit" name="xyz" ... />)
You can check that out easily, navigate to the page where you form is located, and view the html source in your browser, and have a look at the submit buttons.
If they lack a name="xyz" field, then they will not be posted! there are multiple ways to work around this, the easiest i can think of for now is to give them a name manually:

barButton = SubmitButton(attrs={'value':'bar', 'name'='submit'})
(untested)

Give that a try, if this doesnt work, I'll throw together a small sample app and try myself.

Goodluck :)


Jamie Bullock

unread,
Aug 27, 2009, 9:01:50 AM8/27/09
to turbo...@googlegroups.com

Hi Crusty,

On 27 Aug 2009, at 12:13, Crusty wrote:

>
> you named your buttons:
>
> fooButton = SubmitButton(attrs={'value':'foo'})
> barButton = SubmitButton(attrs={'value':'bar'})
>
> so the respective kw fields should be kw['fooButton'] and
> kw['barButton']
> If these don't exist as well it might have the following reason
>
> I am working more with Sprox than with TW, and sprox does _not_ give
> SubmitButtons a name (<input type="submit" name="xyz" ... />)
> You can check that out easily, navigate to the page where you form
> is located, and view the html source in your browser, and have a
> look at the submit buttons.
> If they lack a name="xyz" field, then they will not be posted!

That's correct, they lack name="" altogether.


> there are multiple ways to work around this, the easiest i can think
> of for now is to give them a name manually:
>
> barButton = SubmitButton(attrs={'value':'bar', 'name'='submit'})
> (untested)
>

Ah, this works!

Thanks for the help.

Jamie


Reply all
Reply to author
Forward
0 new messages