How can I access the form submit button value in Django?

Question:

I have a Django project that, on one page, has multiple forms (in different tags) that can be submitted to have different effects. In all cases I want the user to be redirected back to the same page, so I use in my view the pattern of submitting the form and then redirecting to the original page. In at least one case, the only difference between two of the forms is the value of the submit button.

In my view I have the code (which is the first time my view function accesses the request.POST):

if request.POST['submit']=='Add':
    #code to deal with the "Add" form

and in the template, the first form has a submit button like

<input type="submit" value="Add">

I thought this would work, but when I submit that form, I get an error at the line in view from above:

Key ‘submit’ not found in <QueryDict: {u'clientyear': [u'2012'], u'csrfmiddlewaretoken': [u'be1f2f051f09f6ab0375fdf76cf6a4d7'], u'ben': [u'123405']}>

Obviously, this does not have a 'submit' key or any key with the value corresponding to the submit button I clicked. So, since this does not work, how can access the value of the submit button or tell which of the forms has been submitted?

Asked By: murgatroid99

||

Answers:

Submit is an HTML Form structure… You must use name attribute of form objects as follows… In your template:

<form>
...
<input type="submit" name="list" value="List Objects" />
</form>
<form>
...
<input type="submit" name="do-something-else" value="Do Something Else" />
</form>

In your view:

if 'list' in request.POST:
    # do some listing...
elif 'do-something-else' in request.POST:
    # do something else
Answered By: FallenAngel

One thing to keep in mind to prevent confusion. The name of the submit button will not show if there is only a single button in the form.

#template.html
<form action="..." method="post">
<input type="submit" name = "first_button" value="Add">
</form>

#view.py
...
'first_button' in request.POST  #False

#template.html
<form action="..." method="post">
<input type="submit" name = "first_button" value="Add">
<input type="submit" name = "second_button" value="Remove">
</form>

#view.py
...
'first_button' in request.POST  #True if you clicked on that button

I’m little bit late but here is the solution

Problem you are facing

Your are trying to get Button name but getting the initial value of button that is not correct way.

HTML Code

<input type="submit" value="Add">

Python Code/View.py

if request.POST['submit']=='Add':
#code to deal with the "Add" form

Solution

First find button name in request.POST dictionary if exist then get their value.

HTML Code

Add name of your button and their value.

<input type="submit" value="Add" name="add_object">

Views.py

You can find the button name in request.POST dictionary

if request.POST['submit'] == 'add_object':
# Both ways to deal with it
if 'add_object' in request.POST:

Extra Stuff

We have two forms on a page.

First form have 2 buttons with same name subjects but different values fav_HTML and fav_CSS.

Second form also have 2 buttons with same name tutorials but different values
Tutorials_HTML and Tutorials_CSS.

 <form action="" method="post">
      Form 1
      <button name="subject" type="submit" value="interview_HTML">HTML</button>
      <button name="subject" type="submit" value="interview_CSS">CSS</button>
 </form> 

<form action="" method="post">
      Form 2 
      <button name="tutorials" type="submit" value="Tutorials_HTML">HTML</button>
      <button name="tutorials" type="submit" value="Tutorials_CSS">CSS</button>
 </form> 

views.py

We can handle different forms, check which button is clicked then getting their values and do something.

if 'subject' in request.POST: # this section handle subject form (1st Form)
#now we can check which button is clicked 
# Form 1 is submitted , button value is subject now getting their value 

    if 'interview_HTML' == request.POST.get('subject'):
       pass 
       # do something with interview_HTML button is clicked
    elif 'interview_CSS' == request.POST.get('subject'):
        pass
        # do something with interview_CSS button is clicked

elif 'tutorials' in request.POST: #this section handle tutorials form (2nd form)

    #now we can check which button is clicked 
    # Form 1 is submitted , button name is tutorials now getting their value 

    if 'Tutorials_HTML' == request.POST.get('tutorials'):
        pass 
        # do something with fav_HTML button is clicked
    elif 'Tutorials_CSS' == request.POST.get('tutorials'):
        pass
        # do something with fav_CSS button is clicked
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.