如何从django模板中的Jquery脚本调用python函数

时间:2021-07-28 20:19:19

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.

我是django和jquery的新手,我正在使用django1.3搜索jquery的'hello world'示例,当用户按下按钮或加载时,hello world作为字符串/ json从服务器返回到客户端页。

Note: I am using django1.3 and python 2.7.Sorry this is a very basic question.

注意:我使用的是django1.3和python 2.7。这是一个非常基本的问题。

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.

我成功地在`显示一个字符串'这是JQuery的Hello World“没有任何视图函数(仅使用jquery)。但是我不知道如何使用jquery函数中的视图函数如果任何人有想法请帮助我。谢谢advance.om模板我尝试使用以下代码片段。

urls.py

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:

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

如何从jquery调用函数'test'和'hellofromserver'?(来自模板)。

2 个解决方案

#1


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.

......你遇到的问题是什么?您的Javascript控制台中的任何信息?也许你没有设置你的django应用程序来使用CSRF保护。

#2


0  

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:

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

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>

#1


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.

......你遇到的问题是什么?您的Javascript控制台中的任何信息?也许你没有设置你的django应用程序来使用CSRF保护。

#2


0  

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:

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

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>