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

前端 未结 3 1534
春和景丽
春和景丽 2020-12-09 09:19

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 redire

相关标签:
3条回答
  • 2020-12-09 09:22

    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
    
    0 讨论(0)
  • 2020-12-09 09:23

    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
    
    0 讨论(0)
  • 2020-12-09 09:44

    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
    
    0 讨论(0)
提交回复
热议问题