简体   繁体   中英

execute python funtion from within html with django

So, i have a website hosted on a raspberry pi running django. i have my home.html where i want to display a menu made of images. when the images are clicked i want them to run a python script/function. Can anyone help me with this at all??

On another note, i don't want the page to change. it's just one page with images that when clicked execute different code.

I have manage to get the code to run but it runs when the page loads as apposed to when the button is clicked.

Maybe django isn't the answer here?

To execute server code on a client side action, you'll obviously have to make a request.

In your case probably an ajax request since you won't need to reload the page.

Below is a simple example of an ajax request :

<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open('GET', '/yourpath', true); //will trigger http://yourdomain.com/yourpath.
xhr.onreadystatechange = function(e) {
  if (this.readyState == 4 && this.status == 200) { //200 means OK
    console.log(this.responseText); //in your browser : F12 => Console. This is what the server returned.
  }
};

xhr.send();
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM