librelist archives

« back to archive

Is there a proper way to determine what button was pushed on a form

Is there a proper way to determine what button was pushed on a form

From:
Rob Fowler
Date:
2011-11-29 @ 01:25
This is probably a dumb question, but on my form I have two buttons 
(save and cancel) how can I see what button is pressed in my handler?

I am using wtforms but I don't think that makes any difference as the 
buttons are in the template.
I have this in my form: "<p><input type=submit name=save value=Save> 
<input type=submit name=cancel value=Cancel>"
I want to know which one of save or cancel were send
In the form object I can see 'Save' is set when I hit save and 'Cancel' 
is set (ie. form.cancel is u"Cancel")
Am I meant to use a getattr() to see if the field is in the form?
It will work but seems overly low level.
- Rob
ps. thoroughly enjoying flask and wtforms

Re: [flask] Is there a proper way to determine what button was pushed on a form

From:
Louis Roché
Date:
2011-11-29 @ 08:31
Hi.

I have in my template :
<input type="submit" name="btn" value="Save"><input type="submit" name="btn"
value="Cancel">

And to now wich one is push, I have in my .py file:
if request.form['btn'] == 'Save':
    something0
else:
    something1

Louis

2011/11/29 Rob Fowler <legal@mianos.com>

> This is probably a dumb question, but on my form I have two buttons
> (save and cancel) how can I see what button is pressed in my handler?
>
> I am using wtforms but I don't think that makes any difference as the
> buttons are in the template.
> I have this in my form: "<p><input type=submit name=save value=Save>
> <input type=submit name=cancel value=Cancel>"
> I want to know which one of save or cancel were send
> In the form object I can see 'Save' is set when I hit save and 'Cancel'
> is set (ie. form.cancel is u"Cancel")
> Am I meant to use a getattr() to see if the field is in the form?
> It will work but seems overly low level.
> - Rob
> ps. thoroughly enjoying flask and wtforms
>
>
>

Re: [flask] Is there a proper way to determine what button was pushed on a form

From:
Adam Patterson
Date:
2011-11-29 @ 13:26
For a cancel you should probably just use a link instead of a input:submit

No need to do a POST (via cancel) if you are just canceling.

or even just do <input type="button" value="Clear"
onclick="document.location='.';">

Just a thought

On Tue, Nov 29, 2011 at 3:31 PM, Louis Roché <mail@louisroche.net> wrote:
> Hi.
>
> I have in my template :
> <input type="submit" name="btn" value="Save"><input type="submit" name="btn"
> value="Cancel">
>
> And to now wich one is push, I have in my .py file:
> if request.form['btn'] == 'Save':
>     something0
> else:
>     something1
>
> Louis
>
> 2011/11/29 Rob Fowler <legal@mianos.com>
>>
>> This is probably a dumb question, but on my form I have two buttons
>> (save and cancel) how can I see what button is pressed in my handler?
>>
>> I am using wtforms but I don't think that makes any difference as the
>> buttons are in the template.
>> I have this in my form: "<p><input type=submit name=save value=Save>
>> <input type=submit name=cancel value=Cancel>"
>> I want to know which one of save or cancel were send
>> In the form object I can see 'Save' is set when I hit save and 'Cancel'
>> is set (ie. form.cancel is u"Cancel")
>> Am I meant to use a getattr() to see if the field is in the form?
>> It will work but seems overly low level.
>> - Rob
>> ps. thoroughly enjoying flask and wtforms
>>
>>
>
>

Re: [flask] Is there a proper way to determine what button was pushed on a form SOLVED

From:
Rob Fowler
Date:
2011-11-29 @ 10:01
Perfect! I knew there was an easy way. Thanks.

On 29/11/2011 7:31 PM, Louis Roché wrote:
> Hi.
>
> I have in my template :
> 
<inputtype="submit"name="btn"value="Save"><inputtype="submit"name="btn"value="Cancel">
>
> And to now wich one is push, I have in my .py file:
> ifrequest.form['btn']=='Save':
>     something0
> else:
>     something1
>
> Louis
>
> 2011/11/29 Rob Fowler <legal@mianos.com <mailto:legal@mianos.com>>
>
>     This is probably a dumb question, but on my form I have two buttons
>     (save and cancel) how can I see what button is pressed in my handler?
>
>     I am using wtforms but I don't think that makes any difference as the
>     buttons are in the template.
>     I have this in my form: "<p><input type=submit name=save value=Save>
>     <input type=submit name=cancel value=Cancel>"
>     I want to know which one of save or cancel were send
>     In the form object I can see 'Save' is set when I hit save and
>     'Cancel'
>     is set (ie. form.cancel is u"Cancel")
>     Am I meant to use a getattr() to see if the field is in the form?
>     It will work but seems overly low level.
>     - Rob
>     ps. thoroughly enjoying flask and wtforms
>
>
>