简体   繁体   English

如何在HTML和Django中以单一形式使用2个提交按钮?

[英]How can I use 2 submit buttons in a single form in HTML & Django?

In this, only second button is working. 在这种情况下,只有第二个按钮起作用。 If I delete the second button, then the first button is worked.I want to work both buttons.How can I work them? 如果删除第二个按钮,则第一个按钮可以使用,我想同时使用两个按钮,如何使用它们?

HTML template IInd_std.html HTML模板IInd_std.html

  <form method="get" action="#">
    <table style="width:100%">
      <tr>
    <th>Slno</th>
        <th>Name</th>
        <th>Attendance</th> 
        </tr>
      <tr>
    <td>1</td>
        <td>Abijith</td>
        <td>{{ u }}
<input type="submit"  value="present" name="add20"/>
<input type="submit" value="absent" name="add21"/>
{{ v }}
    </td>
      </tr>
    </table>

views.py views.py

u = 1; v = 1
def IInd_std(request):
    global u,v

    my_dictionary = {
    "u" : u, "v" : v
    }
    if request.GET.get('add20'):
        u = u+1
        my_dictionary = {
            "u" : u,
        }
    if request.GET.get('add21'):
        v = v+1
        my_dictionary = {
            "v" : v,
        }


    return render(request, 'blog/IInd_std.html', my_dictionary)

Try changing it to: 尝试将其更改为:

request.GET['add20']:

and

request.GET['add21']:

Also, you are re-initializing my_dictionary in your if statements. 另外,您正在if语句中重新初始化my_dictionary

Change it to: 更改为:

my_dictionary["u"] = u

and

my_dictionary["v"] = v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM