简体   繁体   English

"如何在 Django 中访问表单提交按钮的值?"

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

I have a Django project that, on one page, has multiple forms (in different tags) that can be submitted to have different effects.我有一个 Django 项目,在一个页面上,有多个表单(在不同的标签中)可以提交以产生不同的效果。 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<\/code> ):在我看来,我有代码(这是我的视图函数第一次访问request.POST<\/code> ):

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

Submit<\/code> is an HTML Form structure... You must use name attribute of form objects as follows... In your template: Submit<\/code>是一个 HTML 表单结构...您必须使用表单对象的名称属性,如下所示...在您的模板中:

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

One thing to keep in mind to prevent confusion.要记住一件事以防止混淆。 The name<\/code> of the submit button will not show if there is only a single button in the form.如果表单中只有一个按钮,则不会显示提交按钮的name<\/code> 。

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

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

I'm little bit late but here is the solution我有点晚了,但这是解决方案

Problem you are facing<\/strong>您面临的问题<\/strong>

Your are trying to get Button name but getting the initial value of button that is not correct way.您正在尝试获取按钮名称,但获取按钮的初始值不正确。

HTML Code<\/strong> HTML 代码<\/strong>

<input type="submit" value="Add"><\/code><\/pre><\/blockquote>

Python Code\/View.py<\/strong> Python 代码\/View.py<\/strong>

 if request.POST['submit']=='Add': #code to deal with the "Add" form<\/code><\/pre><\/blockquote>

Solution<\/strong>解决方案<\/strong>

First find button name in request.POST dictionary if exist then get their value.如果存在,首先在 request.POST 字典中找到按钮名称,然后获取它们的值。

HTML Code<\/strong> HTML 代码<\/strong>

Add name of your button and their value.添加按钮的名称及其值。

 <input type="submit" value="Add" name="add_object"><\/code><\/pre><\/blockquote>

Views.py<\/strong>视图.py<\/strong>

You can find the button name in request.POST dictionary您可以在 request.POST 字典中找到按钮名称

if request.POST['submit'] == 'add_object': # Both ways to deal with it if 'add_object' in request.POST:<\/code><\/pre><\/blockquote> 
                           

Extra Stuff<\/strong>额外的东西<\/strong>

We have two forms on a page.我们在一个页面上有两个表单。

First form have 2 buttons with same name subjects<\/strong> but different values fav_HTML<\/strong> and fav_CSS<\/strong> .第一种形式有 2 个按钮,主题<\/strong>名称相同,但值不同fav_HTML<\/strong>和fav_CSS<\/strong> 。

Second form also have 2 buttons with same name tutorials<\/strong> but different values Tutorials_HTML<\/strong> and Tutorials_CSS<\/strong> .第二种形式也有 2 个按钮,同名tutorials<\/strong>但值不同Tutorials_HTML<\/strong>和Tutorials_CSS<\/strong> 。

 <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><\/code><\/pre>

views.py<\/strong>视图.py<\/strong>

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<\/code><\/pre>"

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

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