防止Django中提交多个表单 [英] Prevent multiple form submissions in Django

查看:90
本文介绍了防止Django中提交多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通用方法来防止多个表单提交。我发现这个方法看起来很有希望。而我不想在我的所有观点中包含这个代码段。

I'm looking for a generic way to prevent multiple form submissions. I found this approach which looks promising. Whereas I do not want to include this snippet in all of my views. Its probably easier to do this with a request processor or a middleware.

任何最佳做法建议?

推荐答案

客户端,从JavaScript开始。你永远都不会信任客户,而是一个开始。

Client side, start with JavaScript. You can never trust the client, but its a start.

onclick="this.disabled=true,this.form.submit();

服务器端您可以将数据插入到数据库中,即校验和,如果将其插入到数据库中的记录使用 model.objects.get_or_create()强制数据库级别的唯一性,您应该使用unique_together。

Server side you 'could' insert something into a database i.e. a checksum. If its a records you are insert into a database use model.objects.get_or_create() to force the uniqueness on database level you should use unique_together.

最后:HTTPRedirect是最好的,当用户处理付款时,我使用的方法是向感谢/构造页面发出HTTPRedirect(),这样刷新表单将不会重新提交,如果他们回去尝试提交表单(不刷新表单)Django十字架请求伪造(CSRF)将失败,完美!

Lastly: HTTPRedirect is best, The the method I use when a user processes a payment is to just issue a HTTPRedirect() to the thank you/conformation page. This way refreshing the form won't resubmit and if they go back and try to submit the form again (without refreshing the form) the Django Cross Site Request Forgery (CSRF) will fail, perfect!

这篇关于防止Django中提交多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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