15

I have a django form with two different submit buttons, on the view where the form is submitted to I need to know what submit button was pressed and take different actions accordingly.

From what I have read the submit button's name or id should be somewhere in the request.POST dictionary, but it not there!

This is a fragment of my form:

<form id="editPaperForm" action="{{paper.editURL}}" method="POST">
   <input type="submit" name="savePaperButton" id="savePaperButton" value="Save and Send Later"/>
   <input type="submit" name="sendPaperButton" id="sendPaperButton" value="Save and send"/>

   ...

</form>

In the view:

...
if 'sendPaperButton' in request.POST:
   return applicants_confirmSend(request, paperID)
else:
   return applicants_home(request)

sendPaperButton is never in the request.POST, and neither is the other one, should I be looking somewhere else?

The only idea I have is to add a hidden field and modify it via javascript before sending the form but that seems kind of redundant since I'm pretty sure that data should be there somewhere...

Thanks!

Daniel Gollás
  • 1,044
  • 2
  • 10
  • 18
  • 1
    Have you tried looking at what your browser sends via an http debugger such as Charles or Fiddler? – Bjorn Jan 29 '10 at 04:15
  • No, I have not, but you got me curious about what the browser was sending so I tried a different browser (was using Firefox 3.5, tried Chrome 4 and IE6), turns out Chrome and IE6 do send the button id and value, Firefox does not... guess hidden input with Javascript added value is my only option? – Daniel Gollás Jan 29 '10 at 04:33
  • 1
    Why not use a checkbox or some other proper form element? – Bjorn Jan 29 '10 at 05:12
  • You could try using the button-element instead. But IE6 might have some issues with it if I recall correctly. –  Jan 29 '10 at 12:18
  • I tested with latest Firefox (3.5.7) and it worked fine. – googletorp Jan 29 '10 at 14:35
  • I don't think a checkbox is appropriate for this situation. The two buttons do very different things, and checking a box is an extra step in what should be a simple button press. Anyway, I tried again this morning and now Firefox is sending it... this is very wierd... thankyou all for your suggestions anyway. – Daniel Gollás Jan 29 '10 at 16:46
  • Is there a has_key function on request.POST -- this is a blind guess, but doesn't request.POST contain a list of key/value pairs and not a list if strings? – Nick Bolton Jan 29 '10 at 21:48

2 Answers2

30

Don't forget to add the name and value parameters to your "button" or "input type=submit" fields of the form. I've had the same problem once and it drove me crazy.

In short, as request.POST contains a dict, you need a key and a value. The key corresponds to the name parameter of your button, and the dict's value to the button's value.

<button type="submit" value="preview">Preview</button>

won't be reflected in request.POST (there's no key for the POST dictionary!), whereas

<button type="submit" value="preview" name="preview">Preview</button> 

will have a key "preview" with value "preview".

Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
  • This is exactly what I forgot. Thanks for the hint! – Patrick Jul 09 '10 at 17:23
  • Having trouble in lastest version of Google Chrome with this, although it works fine with the latest version of Firefox. – Pat Sep 18 '12 at 03:38
  • This fixed a similar problem I was having where form fields had unique IDs and no `name`, and the POST contained no data! Was driving me nuts! Thanks! – John Lyon Oct 28 '12 at 05:06
0

For some reason, in Chrome, when I had two buttons using <input/> tags, it would actually treat the button I didn't click as an input. That way, when I tested something like 'sendPaperButton' in request.POST, it would return the opposite of what I wanted.

I changed these to <button></button> tags and it worked fine.

Day
  • 9,465
  • 6
  • 57
  • 93
YPCrumble
  • 26,610
  • 23
  • 107
  • 172