Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django request.POST does not contain the name of the button that submitted the form

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!

like image 507
Daniel Gollás Avatar asked Jan 29 '10 03:01

Daniel Gollás


1 Answers

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".

like image 178
LaundroMat Avatar answered Oct 02 '22 15:10

LaundroMat