Belitho Xavier
Belitho Xavier

Reputation: 139

How can i use button to execute a view in Django?

Im using django to display some data of a database in a table, in that table a have a horizontal partition which separate data by date(old data and new data.) On top of the table ive created 2 button, "partition 1" and "partition 2" so when i click "partition 1" it displays me the data of the old partition. I would like to know how can when i click the button "old" to change the data in the main table(contains both new and old data) to the datas of old partition. enter image description here

Note: I have all views created for 3 of them.

Upvotes: 0

Views: 219

Answers (1)

Bialomazur
Bialomazur

Reputation: 1151

You make it quite difficult for other users to give an appropriate answer to that question, when you do not include any of your source code. However, when you want to execute some sort of view on your backend when a user clicks a button on the frontend, then I would recommend using a javascript ajax request. The request type should be get in this instance, as you've said that the backend should get some sort of data and update the table when the user performs the described action.

Due to the lack of information, I can only offer some basic code for the ajax implementation.

// Javascript
const viewUrl = "someView/"

function callView(){
   $.ajax({
          type:"get",
          url: viewUrl,
          success: (answer) => {//some operation}
   })
}

//Html

<button onclick=callView()>Click me!</button>

Upvotes: 1

Related Questions