Django request.POST不包含提交表单的按钮的名称 [英] Django request.POST does not contain the name of the button that submitted the form

查看:631
本文介绍了Django request.POST不包含提交表单的按钮的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个不同提交按钮的django表单,在表单提交的视图中,我需要知道提交按钮被按下并相应地执行不同的操作。

I have a django form with two different submit buttons, on the view where the form is submitted to I need to know what submit button was pressed and take different actions accordingly.

从我所看到的提交按钮的名称或ID应该在request.POST字典中的某个地方,但它不在那里!

From what I have read the submit button's name or id should be somewhere in the request.POST dictionary, but it not there!

这是我的表单片段:

<form id="editPaperForm" action="{{paper.editURL}}" method="POST">
   <input type="submit" name="savePaperButton" id="savePaperButton" value="Save and Send Later"/>
   <input type="submit" name="sendPaperButton" id="sendPaperButton" value="Save and send"/>

   ...

</form>

在视图中:

...
if 'sendPaperButton' in request.POST:
   return applicants_confirmSend(request, paperID)
else:
   return applicants_home(request)

sendPaperButton从不在request.POST中,而另一个也不是,我应该在别的地方看?

sendPaperButton is never in the request.POST, and neither is the other one, should I be looking somewhere else?

我唯一的想法是在发送表单之前添加一个隐藏的字段并通过javascript进行修改,但是似乎有些冗余,因为我很确定数据应该在某处...

The only idea I have is to add a hidden field and modify it via javascript before sending the form but that seems kind of redundant since I'm pretty sure that data should be there somewhere...

谢谢!

推荐答案

Don不要忘记在表单的按钮或输入类型=提交字段中添加名称和值参数。我曾遇到同样的问题,让我疯狂。

Don't forget to add the name and value parameters to your "button" or "input type=submit" fields of the form. I've had the same problem once and it drove me crazy.

简而言之,as request.POST包含一个dict,你需要一个键和一个值。键对应于您的按钮的名称参数,以及dict的按钮值的值。

In short, as request.POST contains a dict, you need a key and a value. The key corresponds to the name parameter of your button, and the dict's value to the button's value.

<button type="submit" value="preview">Preview</button>

不会反映在request.POST中(POST字典没有键),而

won't be reflected in request.POST (there's no key for the POST dictionary!), whereas

<button type="submit" value="preview" name="preview">Preview</button> 

将有一个带有预览值的预览键。

will have a key "preview" with value "preview".

这篇关于Django request.POST不包含提交表单的按钮的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆