2012-09-11 111 views
0

为django电子商务页面创建付款表单,我遇到了处理保存的信用卡信息的情况。覆盖HTML <input type = hidden>值

如果我想告诉我的服务器使用什么动作,我是否需要将第一个输入和按钮封装在自己的窗体中?或者我可以使用下面的代码?

{% if user.is_authenticated and user.get_profile.stripe_customer_id %} 
     <input type="hidden" name="action=" value="delete_card"/> 
     <button type="submit">Use Another Card</button> 

     <input type="hidden" name="action=" value="pay_saved_card"/> 


{% endif %} 

回答

0

一般来说,对于要提交或取消的表单,会在表单中添加以下内容。

<input type="submit" name="action" value="Cancel" /> 
<input type="submit" name="action" value="Save" /> 

而且取决于在Django request.POST['action']查看你要么你处理保存表单或做取消处理。

+0

不要认为这是个好主意。如果用户只是在输入栏中按'enter'会发生什么? –

0

您不能用HTML覆盖隐藏字段,除非您可以使另一个控件发送具有相同名称的参数,然后服务器端检查情况。

的情况下,最简单的方法是有一个像

<input type="checkbox" name="action" value="delete_card" id="othercard" /> 
<label for="othercard">Use Another Card</label> 

复选框不使用隐藏域。然后服务器端代码应该只是测试action属性的值delete_card的存在。

使用name="action="中的等号可能并不常见或不需要:提交表单时,会生成表单name = value的数据项,因此您将得到action==delete_card

+0

任何浏览器是否放弃重复的名称参数? – lol