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

39👍

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
👤Mp0int

12👍

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

10👍

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

Leave a comment