0

I am building a web interface using Django and want to run some code of python on button click event of bootstrap button. I want to know in which python file should I write that code and how to call that function.

The code I am trying to run is using subProcess module of python to run a command on terminal and get error and output streams. The code it self is working fine in python 3.7 but I don't know how to call that code on button click. I am using django for the first time.

<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>
  • 1
    You don't call Python files. You call URLs. Write a Django view and add it to a path in urls.py. – Daniel Roseman Sep 24 '19 at 08:28
  • Hello and welcome to SO. You may want to read about [the difference between client-side and server-side code in web programming](https://stackoverflow.com/q/13840429/41316). – bruno desthuilliers Sep 24 '19 at 08:36

1 Answers1

0

1 - Python runs on server side, you can call with Ajax and return the data. (Python)

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

2 - Set id to button and call with jQuery when you click on it. (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>