I'd like to execute some Python code if that button is pressed on web.The thing is, I need the page not to redirect or refresh or anything.
Asked
Active
Viewed 2,379 times
0
-
1You might want to vote up the answers that are helpful to you. – Mingyu Aug 28 '13 at 15:08
-
He can't, not enough rep ;) – avalancha Feb 18 '14 at 07:16
3 Answers
4
Just use jQuery ajax,it is easy to do
in django views.py
def fun1(request):
user_input = request.GET.get('value')
#put your code here
return HttpResponse('what you want to output to web')
urls.py
url(r'^link-to-fun1$', views.fun1),
html
<html>
<head>
<title>Your title</title>
<script type="text/javascript" src="/media/js/jquery-1.10.2.min.js"></script>
<script>
function myFun() {
$.get('link-to-fun1/',{"value":"get_the_value_from_web"},function(ret) {
return ret;//you can handle with return value ret here
});
}
</script>
</head>
<body>
More see jQuery $.get method

WeizhongTu
- 6,124
- 4
- 37
- 51
-
What do you mean with url(r'^link-to-fun1$', views.fun1), ? What should I put in link-to-fun1$? – user1919 May 31 '16 at 14:21
1
there's a lot of info on internet about django and ajax, just google it.
http://www.youtube.com/watch?v=lllVAFbRGfI
or as Mingyu said use dajax

montiniz
- 960
- 8
- 7
0
You may want to try the Ajax library for Django project:
In case you've never heard of Ajax, here's the definition from Wikipedia:
Ajax (an acronym for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create asynchronous web applications.
With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page.

Mingyu
- 31,751
- 14
- 55
- 60
-
1$.get(url,function(){}) and add a new page in django to return data,but you provide a new method,Thank you! – WeizhongTu Aug 28 '13 at 13:35