简体   繁体   中英

Use django view without redirecting or refreshing a page

I have a button next to some text on a page. I'd like to execute some Python code if that button is pressed. The only way I know how to do this is through a view. The thing is, I need the page not to redirect or refresh or anything, just some code to execute when the button is pressed. Any ideas on how I'd get this done?

Asynchronous Javascript and XML (Ajax).

You can perform an Ajax request to the view you want to execute and this will be done without refreshing nor redirecting the current web page.

This is the W3C Ajax tutorial which might be good for beginners. All you have to do is code some javascript in your templates, add the onclick event listener to your button and you're up :)

You may take a look at this tutorials and documentation , seems very apropiate.

Hope this helps!

When button is clicked you can use javascript to make an async request (AJAX) to your django project

typing in django ajax to google shows a lot of resources, one of which is a beginners tutorial: http://lethain.com/two-faced-django-part-5-jquery-ajax/

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