【问题标题】:Calling a python function from bootstrap button in django web frame work从 django web 框架中的引导按钮调用 python 函数
【发布时间】:2020-01-24 07:28:01
【问题描述】:

我正在使用 Django 构建一个 Web 界面,并希望在引导按钮的按钮单击事件上运行一些 python 代码。我想知道我应该在哪个 python 文件中编写该代码以及如何调用该函数。

我尝试运行的代码是使用 python 的 subProcess 模块在终端上运行命令并获取错误和输出流。它自己的代码在 python 3.7 中运行良好,但我不知道如何在按钮单击时调用该代码。我是第一次使用 django。

<button type="button" class="btn btn-primary" name="sra" onclick="myFunction()">Primary</button>
<script>
function myFunction(){
#this is the part where I am stuck
}
</script>

【问题讨论】:

标签: django python-3.x bootstrap-4 onclick


【解决方案1】:

1 - Python 在服务器端运行,您可以使用 Ajax 调用并返回数据。 (Python)

$.ajax({
    url: 'url',
    data: {},
    dataType: 'json',
    success: function (data) {
      ...
      }
  });

2 - 将 id 设置为按钮并在单击时使用 jQuery 调用。 (Javascript)

<button id=“myButton” type="button" class="btn btn-primary">Primary</button>

<script>
  $(“#myButton”).on(“click”, function(){
    #this is the part where I am stuck
  })
</script>

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2020-09-28
    • 2016-01-31
    • 2017-03-27
    相关资源
    最近更新 更多