Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

python - 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.

question from:https://stackoverflow.com/questions/65908757/how-can-i-use-button-to-execute-a-view-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...