0

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.

WeizhongTu
  • 6,124
  • 4
  • 37
  • 51

3 Answers3

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
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:

http://www.dajaxproject.com/

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