gpt4 book ai didi

python - Django Views - 阻止连续快速调用

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:21 28 4
gpt4 key购买 nike

在我的应用程序中单击按钮会调用一个 View ,该 View 会进行一些数据库更改,然后重定向到呈现 html 的新 View 。当用户通常点击链接时,他会在几秒钟内不小心点击两次到三次。如果不到 10 秒前进行了相同的调用,我想阻止 View 调用。当然我可以通过检查数据库来做到这一点,但我希望通过在 django 中使用一些装饰器来获得更快的解决方案。

最佳答案

这应该有所帮助。这是一个 JavaScript,用于在单击时禁用按钮,在操作完成后启用它。

$(document).ready(function () {
$("#btn").on("click", function() {
$(this).attr("disabled", "disabled");
doWork(); //this method contains your logic
});
});

function doWork() {
alert("doing work");
//actually this function will do something and when processing is done the button is enabled by removing the 'disabled' attribute
//I use setTimeout so you can see the button can only be clicked once, and can't be clicked again while work is being done
setTimeout('$("#btn").removeAttr("disabled")', 1500);
}

更多信息 here .

关于python - Django Views - 阻止连续快速调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061299/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com