Reputation: 1806
I have multiple form on the same page. Each form as a submit button that look like that:
<button type="submit" name="button_name" style="display:none;" class="btn btn-primary btn-striped" disabled="disabled"><span class="glyphicon glyphicon-floppy-saved"></span><span class="button-text"> {% trans "Save" %}</span></button>
When I click on the save button, everything works OK, I can distinguish in the view which form as been posted like that :
if request.method == 'POST':
if 'button_name' in request.POST:
[...]
elif 'button_name_2' in request.POST:
[...]
But if I press the enter/return key once I have completed a text field, the form is posted but I got no button name in the request.POST dictionnary. The only buttons I have ont the page are submit button and they all have type="submit" name="button_name"
in it.
Upvotes: 0
Views: 873
Reputation: 188
Why don't you try something like this...
if request.method == 'POST':
#get the button name or None
button_name = request.POST.get("button_name",None)
button_name2 = request.POST.get("button_name2",None)
#if you found button_name do something
if button_name:
[...]
#else if you found button_name2 do something else
elif button_name2:
[...]
Upvotes: 0
Reputation: 308899
The button name is only included in the form data if the button was used to submit the form. If you submit the form using return, then the button name will not be included.
If you wish to distinguish between different forms, then you could add a hidden input to each form.
<form>
...
<input name='form1' type='hidden' />
</form>
<form>
...
<input name='form2' type='hidden' />
</form>
Then in your view you can check for the hidden input.
if request.method == 'POST':
if 'form1' in request.POST:
...
elif 'form2' in request.POST:
...
Upvotes: 2