阿贾克斯邮政Django框架? [英] Ajax Post in Django framework?
问题描述
我放在一起的Django框架内的AJAX / jQuery的后一个简单的测试,但真的不明白,为什么输出不让它到模板页面。有人吗?
我能看到的职位Firebug的响应选项卡中的内容,但我是否尝试返回一个模板或一个简单的消息,什么也没有发生在浏览器本身。相反,非Ajax后按预期工作(加载新的一页,帖子消息)
我是一个完整的新手到AJAX / jQuery的/ Django的,所以请原谅我的无知:)
最后,我希望能够做的是任意的,非表单变量传递给jQuery通过Django视图。可能?谢谢:)
这里的code -
test.html的:
< HTML>
< HEAD>
&LT;脚本类型=文/ JavaScript的 src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript>
&LT;脚本类型=文/ JavaScript的&GT;
$(文件)。就绪(函数(){
$(#了testForm)。递交(函数(事件){
。事件preventDefault();
$阿贾克斯({
键入:POST,
网址:/ test_results /
});
});
返回false;
});
&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
&LT;形式ID =了testForm行动=/ test_results /方法=邮报&GT;
&LT;输入类型=提交ID =走出去NAME =走出去的价值=&GT去!;
&LT; /形式GT;
&LT; /身体GT;
&LT; / HTML&GT;
views.py:
从django.shortcuts进口render_to_response
从django.http进口的Htt presponse
高清test_ajax(要求):
如果request.is_ajax():
消息=是的,AJAX!
其他:
消息=不阿贾克斯
返回的Htt presponse(消息)
#alternative测试:返回render_to_response('test_results.html)
urls.py:
(R'^ test_results / $',views.test_ajax)
(R'^测试/ $',views.test),
和几乎空白test_results.html:
&LT; HTML&GT;
&LT; HEAD&GT;
&LT; /头&GT;
&LT;身体GT;
检测结果
&LT; /身体GT;
&LT; / HTML&GT;
我可以看到的帖子内容 Firebug的响应选项卡中,但无论是 我试图返回一个模板或简单 消息,什么也没有发生在 浏览器本身。相反,非Ajax 后工作正常(加载新 页面,帖子消息)
如果你得到一个回应,你得到的响应。你只是没有做任何事的。
为什么不提醒数据并将其追加到&LT;身体GT;
例如:
$。阿贾克斯({
键入:POST,
网址:/ test_results /,
数据: {
任意的数据:这是任意的数据,
有的外形场:$(MyForm的输入:一是)。VAL()//从形式
背景颜色:$(机构)的CSS(背景色)。
//所有这些数据通过POST提交您的看法。
//在Django,request.POST ['背景色']
},
成功:功能(数据){
警报(数据);
$(机构)追加(数据);
}
});
I put together a simple test of an ajax/jquery post within a django framework, but don't really understand why the output doesn't make it to a template page. Anyone?
I can see the contents of the post in firebug's 'response' tab, but whether I try to return a template or a simple message, nothing happens in the browser itself. Conversely, a non-ajax post works as expected (loads new page, posts message)
I'm a complete newbie to ajax/jquery/django so please excuse my ignorance :)
Eventually, what I'd like to be able to do is to pass arbitrary, non-form variables to a django view via jquery. Possible? Thank you :)
Here's the code --
test.html:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript>
<script type="text/javascript">
$(document).ready(function() {
$("#testForm").submit(function(event){
event.preventDefault();
$.ajax({
type:"POST",
url:"/test_results/"
});
});
return false;
});
</script>
</head>
<body>
<form id="testForm" action="/test_results/" method="post">
<input type="submit" id="go" name="go" value="Go!">
</form>
</body>
</html>
views.py:
from django.shortcuts import render_to_response
from django.http import HttpResponse
def test_ajax(request):
if request.is_ajax():
message = "Yes, AJAX!"
else:
message = "Not Ajax"
return HttpResponse(message)
#alternative test: return render_to_response('test_results.html')
urls.py:
(r'^test_results/$', views.test_ajax),
(r'^test/$', views.test),
and the almost empty test_results.html:
<html>
<head>
</head>
<body>
test results
</body>
</html>
I can see the contents of the post in firebug's 'response' tab, but whether I try to return a template or a simple message, nothing happens in the browser itself. Conversely, a non-ajax post works as expected (loads new page, posts message)
If you're getting a response, you're getting a response. You're just not doing anything with it.
Why not alert the data and append it to the <body>
for example:
$.ajax({
type:"POST",
url:"/test_results/",
data: {
'arbitrary-data': 'this is arbitrary data',
'some-form-field': $("myform input:first").val(), // from form
'background-color': $("body").css("background-color")
// all of this data is submitted via POST to your view.
// in django, request.POST['background-color']
},
success: function(data){
alert(data);
$("body").append(data);
}
});
这篇关于阿贾克斯邮政Django框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!