one form, many submits

৭৩টি ভিউ
প্রথম অপঠিত মেসেজটিতে চলে আসুন

Tom

পড়া হয়নি,
২০ ফেব, ২০১০, ৯:৪২:১১ AM২০/২/১০
প্রাপক Django users
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?

The only likely property I can see is QUERY_STRING, but I cannot seem
to come up with any info on what this is or how to set it.

Any thoughts would be greatly appreciated.

Thanks,

Tom

David De La Harpe Golden

পড়া হয়নি,
২০ ফেব, ২০১০, ১০:৩৭:৫৭ AM২০/২/১০
প্রাপক django...@googlegroups.com
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"/>

Tom

পড়া হয়নি,
২০ ফেব, ২০১০, ১১:৪৫:২৬ AM২০/২/১০
প্রাপক Django users
David, thanks for that. I ended up going for the simple HTML method
because, as you guessed, the SubmitField idea was a bit unwieldy for
my needs.

Thanks for your help!

Tom

On 20 Feb, 15:37, David De La Harpe Golden


<david.delaharpe.gol...@ichec.ie> wrote:
> 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

Timothy Kinney

পড়া হয়নি,
২০ ফেব, ২০১০, ৫:১৩:৩১ PM২০/২/১০
প্রাপক django...@googlegroups.com
You could also go with a column called "Execute" and a checkbox for the actions you want to execute and then a single submit button that grabs all the checked actions. But I think the other way is probably more intuitive.

-Tim


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


Gunpreet Ahuja

পড়া হয়নি,
২৪ জুন, ২০১৪, ১:৫১:২৩ PM২৪/৬/১৪
প্রাপক django...@googlegroups.com,david.delah...@ichec.ie


On Saturday, February 20, 2010 9:07:57 PM UTC+5:30, David De La Harpe Golden wrote:
On Sat, Feb 20, 2010 at 06:42:11AM -0800, Tom wrote:

 

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

 This was really helpful. :)

Gunpreet Ahuja

পড়া হয়নি,
২৬ জুন, ২০১৪, ৬:৫৩:২৩ AM২৬/৬/১৪
প্রাপক django...@googlegroups.com,david.delah...@ichec.ie


On Tuesday, June 24, 2014 11:21:23 PM UTC+5:30, Gunpreet Ahuja wrote:


On Saturday, February 20, 2010 9:07:57 PM UTC+5:30, David De La Harpe Golden wrote:
On Sat, Feb 20, 2010 at 06:42:11AM -0800, Tom wrote:

 

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


I tried this, the else part works, but the interpreter never considers the above condition true and the control never transfers inside the if clause! Please help!
Here is my views.py:
https://gist.github.com/GunpreetAhuja/cd5093b4e3cbf594b584

Roman Klesel

পড়া হয়নি,
২৬ জুন, ২০১৪, ৯:৩৯:৩৬ AM২৬/৬/১৪
প্রাপক django...@googlegroups.com
From your code:
"""
if request.method=='GET':
if request.POST.get('accept'):
bid = QuotedItem.objects.get(id=2)
return render(request, 'bills/p_bill.html', {'bid' : bid})
"""

if the method is "GET", you will never have any POST parameters.
If you want POST parameter you have to send an HTTP-POST request, but
then of course the first condition will never be true. So this code
will not work.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/324c5116-9030-4afb-8039-ba5f24b7d3fb%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
সকলকে উত্তর দিন
লেখককে লিখে পাঠান
ফরওয়ার্ড করুন
0টি নতুন মেসেজ