0

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).

1
  • I just need to know how jquery communicate to server? Commented Oct 4, 2011 at 15:21

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

In case of 'post' include the code from django projectinto your script docs.djangoproject.com/en/1.3/ref/contrib/csrf/#ajax
0

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

1 Comment

,Thank you answer.Is other parts of my code is ok? .Is following is the way to call a python fn from jquery. 1)$.getJSON("127.0.0.1:8000/viewname?callback= 2)$.getJSON("127.0.0.1:8000/url?callback=

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.