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

Jisson
  • 3,566
  • 8
  • 38
  • 71

2 Answers2

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>
IAbstract
  • 19,551
  • 15
  • 98
  • 146
Jisson
  • 3,566
  • 8
  • 38
  • 71
  • In case of 'post' include the code from django projectinto your script https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#ajax – Jisson Oct 06 '11 at 13:09
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.

rekamoyka
  • 1,122
  • 11
  • 15
  • ,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("http://127.0.0.1:8000/viewname?callback= 2)$.getJSON("http://127.0.0.1:8000/url?callback= – Jisson Oct 01 '11 at 09:47