Django request.POST不包含提交表单的按钮的名称

栏目: Python · 发布时间: 7年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/2159795/django-request-post-does-not-contain-the-name-of-the-button-that-submitted-the-f

我有一个django表单与两个不同的提交按钮,在表单提交的视图,我需要知道什么提交按钮被按下并采取不同的行动相应.

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

这是我的形式的片段:

<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中,而另一个也不是,我应该在别的地方看吗?

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

谢谢!

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

总之,作为request.POST包含一个dict,你需要一个键和一个值.键对应于您的按钮的名称参数,并且dict的值与按钮​​的值相对应.

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

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

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

将有一个关键“预览”与值“预览”.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/2159795/django-request-post-does-not-contain-the-name-of-the-button-that-submitted-the-f


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具