how call a python function from Jquery script in django template
am new to django and jquery,I am searching for a 'hello world' sample for jquery
using django1.3, Where hello world is returned as a string /json from the server to the client when user press a button or at loading of the page.
Note: I am using django1.3 and python 2.7.Sorry this is a very basic question.
I successed in a `shows a string "This is Hello World by JQuery" with out any view functions(using jquery only) .But am unaware of how to use view functions from jquery functions If any one have idea please help me.Thanks in advance.om templates I tried using following code snippets.
urls.py
(r'^hellofromserver/$',views.test),
def test(request):
if request.method == 'GET':
json = simplejson.dumps('hello world!')
return HttpResponse(json, mimetype = 'application/json')
开发者_开发百科jqueyhelloserver.html:
<html>
<head>
<title>jQuery Hello World</title>
<script type="text/javascript" src="{{STATIC_URL}}js/jquery-1.2.6.min.js"></script>
</head>
<body>
.....
<script type="text/javascript">
# how can I get'hello world' from test function(What is the sytax )
</script>
<div id="msgid">
</div>
</body>
</html>
How can I call the functions 'test' and 'hellofromserver' from jquery?(from templates).
Finally I got a 'hello' from the server!
urls.py:
from django.views.generic.simple import direct_to_template
(r'^hello/$',direct_to_template, {'template': 'jqueryserver.html'}),
(r'^jqueryserver/$',views.jqueryserver),
views.py:
#other imports
from django.views.decorators.csrf import csrf_exempt
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
def jqueryserver(request):
print "in jqueryserver"
response_string="hello"
if request.method == 'GET':
if request.is_ajax()== True:
return HttpResponse(response_string,mimetype='text/plain')
jqueryserver.html
<html>
<head>
<title>jQuery Hello World</title>
<script type="text/javascript" src="{{STATIC_URL}}js/jquery.js"></script>
<script>
$.ajax({
type: "GET",
url: "/jqueryserver/",
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<form method ="GET" >
<input type= "submit" id = "save" value ="Save" />
</form>
<div id= "test"></div>
</body>
</html>
...And what's the problem you're having? Any information from your Javascript console? Maybe you haven't set up your django app to use CSRF protection.
精彩评论