Name of button pressed not in request.POST

2,594 views
Skip to first unread message

ydjango

unread,
Oct 4, 2008, 2:44:28 PM10/4/08
to Django users
I do not see the name of submit button in request.POST. it has all
other form fields

if request.POST.has_key('save'):
does not work too

Where can I find which button was pressed.


<form>
various fields...
<input type="submit" id="submit" name="save" value="Save Case"/>
other input buttons
</form>

ydjango

unread,
Oct 5, 2008, 10:40:42 AM10/5/08
to Django users
I have more than one input submit button and want to know which one
was clicked.
Request.POST does not have that information for some reason.

Using django 1.0 svn

I would appreciate if you can point me to right resource.
This is should be something very simple that I am missing.

bruno desthuilliers

unread,
Oct 5, 2008, 1:38:05 PM10/5/08
to Django users
On 5 oct, 16:40, ydjango <neerash...@gmail.com> wrote:
> I have more than one input submit button and want to know which one
> was clicked.
> Request.POST does not have that information for some reason.
>
> Using django 1.0 svn
>
> I would appreciate if you can point me to right resource.
> This is should be something very simple that I am missing.

Probably - but I have no cue about what's causing your problem.


I just did a quick test and it worked just fine. Here's the test:

# yadda/view.py
from django.shortcuts import render_to_response

def form(request):
context = dict(post=dict(), results=list())

if request.POST:
for k in request.POST:
context['post'][k] = request.POST[k]

for name in ('save', 'id_save', 'Save', 'cancel', 'id_cancel',
'Cancel'):
context['results'].append(
"request.POST.has_key('%s') == %s" % (name,
request.POST.has_key(name))
)

return render_to_response('yadda/form.html', context)

# templates/yadda/form.html
{% if results %}
<ul>
{% for r in results %}
<li>{{ r }}</li>
{% endfor %}
<ul>
{% endif %}

{% if post %}
<pre>{{ post|default:"???" }}</pre>
{% else %}
<b>no post</b><br />
{% endif %}

<form method='post' action=''>
<p>
<input type='submit' name='save' id='id_save' value='Save' />
</p>
<p>
<input type='submit' name='cancel' id='id_cancel' value='Cancel' />
</p>
</form>

# urls.py
from django.conf.urls.defaults import *
from yadda.views import form

urlpatterns = patterns('',
url(r'^form/$', form),
)

Try running this in your project...

Malcolm Tredinnick

unread,
Oct 5, 2008, 9:15:55 PM10/5/08
to django...@googlegroups.com

On Sun, 2008-10-05 at 07:40 -0700, ydjango wrote:
> I have more than one input submit button and want to know which one
> was clicked.
> Request.POST does not have that information for some reason.

So you mean you have the same problem you had when you first posted this
message less than one day earlier? Please wait a little while for people
who might have the time to answer your question. Remember that everybody
posting on this list is doing so as a volunteer.

>
> Using django 1.0 svn
>
> I would appreciate if you can point me to right resource.
> This is should be something very simple that I am missing.
>
>
> On Oct 4, 11:44 am, ydjango <neerash...@gmail.com> wrote:
> > I do not see the name of submit button in request.POST. it has all
> > other form fields
> >
> > if request.POST.has_key('save'):
> > does not work too

That's exactly how it should be done. There must be something special or
slightly wrong about the way you've constructed your form or your view.
Perhaps you could construct a very small example (a form with only one
buttone and the corresponding view function) that doesn't work and paste
it in full -- it should only be a dozen lines or so total. Then we might
be able to see what you're doing wrong.

Based on the information you have provided so far, this should work.

Have you looked at what keys *are* being submitted in request.POST?
Maybe that will provide a clue.

Regards,
Malcolm


Glen

unread,
Oct 21, 2008, 2:23:43 PM10/21/08
to Django users
I know this is an old thread. However, I found it when searching
online when I had the same problem. I did quite a bit of other
searching before I finally realized what was happening.

Yes, I see the same behavior where the 'submit.x' and 'submit.y'
values were in the request.POST dictionary, but there was no 'submit'
key. I am not certain if this is expected behavior for either normal
HTTP communication or Django, but the 'submit' (or whatever name you
gave the submit button) key is not in the dictionary if the associated
value in HTML is ''.

In my particular case, I was doing something like this:

<input id='employee_services4_cancel_button'
class='employee_services4_buttons'
type='image'
src='{{MEDIA_URL}}images/cancel_button.gif'
value='{{cancel_button}}'
alt='[{% trans "Cancel" %}]'
name='submit' />

What's relevant is the 'value'. I had forgotten to send the
'cancel_button' variable, and therefore the value was ''. This means
the key does not show in the dictionary.

Although I don't know expected behavior, I would personally had
expected to see the 'submit' entry present in the request.POST, but
the value would have been ''. It may be a bug, but without knowing
expected behavior for both browsers and Django, I cannot say for
certain.



On Oct 4, 11:44 am, ydjango <neerash...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages