gpt4 book ai didi

python - 从模板中的标签脚本调用 Django python 函数

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:32 27 4
gpt4 key购买 nike

这是我的 Django 模板的一些内容。

...
<a id="ID" href="">Do operation with database.<a/>
...
<script type="text/javascript">
window.onload = function () {
var a = document.getElementById("ID");
a.onclick = function () {
if (confirm("do you really want to perform task?")) {
/** call python function in order to perform some operations with database **/
}
return;
}
</script>
...

该函数例如(我们可以想象该函数在views.py中):
def PerformTask():
#对数据库进行操作

我的问题是,如何从模板中的脚本标记调用 PerformTask() 函数?

最佳答案

你应该使用ajax

   $.ajax({
url: "http://abc,com/app/",
type: "POST",
data :{},
success:function(result){
//your code
},
error:function(error){
//error handal
}
});

您可以在此处获取更多详细信息:

https://realpython.com/blog/python/django-and-ajax-form-submissions/

https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

关于python - 从模板中的标签脚本调用 Django python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46425837/

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