On Sat, Feb 20, 2010 at 06:42:11AM -0800, Tom wrote:
> Hi all,
> 
> I have a view that iterates over a queryset to produce a list of
> items.  I have added a checkbox next to each item (from within the
> template) and have multiple 'submit' buttons that will do different
> things with the items selected.  For example, one button will delete
> all the items selected.
> 
> My question is how can I detect which of the submit buttons has been
> pressed?  Is there some property of 'request' that I can access that
> will tell my view which action to peform on the selected items?
> 

That is more a html thing than a django thing.

Non-stone-age browsers will pass  the particular submit button used 
for a form with multiple submits - and only that button - as only
that button is  "successful" (IIRC old IE used to do some wierd/dumb 
thing involving the name and/or value but I've largely suppressed the 
memory...)

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2

(aside: You'll also still come across some ancient html tutorials claiming
forms are allowed exactly one submit button, that is untrue)
http://www.w3.org/TR/html401/interact/forms.html#submit-button

i.e. given a POSTed form with two submit buttons

<input type="submit" name="cancel" value="Cancel"/>
<input type="submit" name="accept" value="Accept"/>

Check request.POST.get('cancel') to see if cancel was clicked,
and request.POST.get('accept') to see if accept was clicked.

(remember someone curious can nonetheless construct a post request 
with both accept and cancel just to see if your server logic
falls over in exploitable fashion)

While django doesn't actually handle input type=submit for you,
there are "SubmitField" snippets floating about. They may have advantages
(e.g. form prefix handling, could in theory refuse to validate 
if more than one SubmitField of the form was successful from a 
malicious client, and maybe supply workarounds for that IE annoyance
I only vaguely recollect),  but may be overkill - you could e.g.  just
pass a "submit" dictionary down to the template, with a key and value
to use as the name of various submit buttons (just to avoid
hardcoding in the template) i.e. the above becomes (assuming you've
stuck the submit dictionary on the form):

<input type="submit" name="{{form.submit.cancel}}" value="Cancel"/>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to