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?
1 Answer 1
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>
viewand that said view returns a http response resulting in a page reload, you have to make an ajax call to the view to trigger whatever code you want to run. That would leave your page unchanged...