Skip to content Skip to sidebar Skip to footer

Django Ajax No Refresh:django View Without Redirecting Or Refreshing A Page

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.

Solution 1:

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


Solution 2:

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


Solution 3:

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.


Post a Comment for "Django Ajax No Refresh:django View Without Redirecting Or Refreshing A Page"