django中的ajax实现(GET)
来源:互联网 发布:propolinse漱口水知乎 编辑:程序博客网 时间:2024/05/17 01:33
如果看过Jquery的ajax文档,很容易在各平台使用他们,ajax也不算是什么新鲜的事了,在Django中结合Jquery使用Ajax,让Django开发的应用发挥更好的用户体验,同时降低服务器的负担,也不是难事。
来看Jquery下Ajax的使用说明,http://api.jquery.com/category/ajax/,哗,有十几个函数,如果想更生动学习,请移步到lulu studio:http://www.cnblogs.com/QLeelulu/archive/2008/04/21/1163021.html
以下只挑jQuery.get() jQuery.post() jQuery.getJSON()三个函数来做例子,Django应用围绕这三个函数讲解:
jQuery.get()也可以更简洁为:$.get(),官文使用例子:
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
例子$.get()两个参数,一个url和一个函数,函数处理返回数据data。data到底是什么,alert出来的内容便是了。它是url返回的html代码,
django提供url,返回一个html页面,比如一篇博客的标题和内容的html片段,然后$.get()对html进行处理,把html片段填充到一个div容器中,那就构成了一个完整的ajax get操作了,代码表演吧:
定义一个url函数,根据blog id返回一篇博客html片段:
url(r'^ajax_get_one_blog/$', 'ajax_get_one_blog',name="ajax_get_one_blog"),
views定义ajax_get_one_blog python函数:
def ajax_get_one_blog(request,template_name="blog/ajax_get_one_blog.html"):
ctx=dict()
id=request.GET.get("id")
blog=get_object_or_404(Article,pk=int(id))
ctx["obj"]=blog
return render_to_response(template_name,ctx,context_instance=RequestContext(request))
其中ajax_get_one_blog的request.GET.get("id")是通过$.get()提供:
function get_one_blog(id)
{
var container = $('#blog_container');
container.html("loading...");
$.get('{%url ajax_get_one_blog%}',{'id':id}, function(ret) {
container.html(ret);
});
}
通过$.get()的第二个参数{'id':id}给服务器端函数ajax_get_one_blog提供id参数,第一个参数是url,第三个函数function(ret),已经从服务器端返回了html片段,最后填充到div中,整个无刷新过程就算完毕。
来看Jquery下Ajax的使用说明,http://api.jquery.com/category/ajax/,哗,有十几个函数,如果想更生动学习,请移步到lulu studio:http://www.cnblogs.com/QLeelulu/archive/2008/04/21/1163021.html
以下只挑jQuery.get() jQuery.post() jQuery.getJSON()三个函数来做例子,Django应用围绕这三个函数讲解:
jQuery.get()也可以更简洁为:$.get(),官文使用例子:
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
例子$.get()两个参数,一个url和一个函数,函数处理返回数据data。data到底是什么,alert出来的内容便是了。它是url返回的html代码,
django提供url,返回一个html页面,比如一篇博客的标题和内容的html片段,然后$.get()对html进行处理,把html片段填充到一个div容器中,那就构成了一个完整的ajax get操作了,代码表演吧:
定义一个url函数,根据blog id返回一篇博客html片段:
url(r'^ajax_get_one_blog/$', 'ajax_get_one_blog',name="ajax_get_one_blog"),
views定义ajax_get_one_blog python函数:
def ajax_get_one_blog(request,template_name="blog/ajax_get_one_blog.html"):
ctx=dict()
id=request.GET.get("id")
blog=get_object_or_404(Article,pk=int(id))
ctx["obj"]=blog
return render_to_response(template_name,ctx,context_instance=RequestContext(request))
其中ajax_get_one_blog的request.GET.get("id")是通过$.get()提供:
function get_one_blog(id)
{
var container = $('#blog_container');
container.html("loading...");
$.get('{%url ajax_get_one_blog%}',{'id':id}, function(ret) {
container.html(ret);
});
}
通过$.get()的第二个参数{'id':id}给服务器端函数ajax_get_one_blog提供id参数,第一个参数是url,第三个函数function(ret),已经从服务器端返回了html片段,最后填充到div中,整个无刷新过程就算完毕。
人口当然是客户端的一个a链接:
转自:http://pythoner.org/wiki/44/
0 0
- django中的ajax实现(GET)
- django中的ajax实现(GET)--转载备份
- django 1.4 利用jquery实现ajax ‘get|post’异步请求
- django中的ajax实现(POST)--转载备份
- django 中的ajax
- Django中的GET和POST
- django实现注册 登录 ajax
- django+ajax实现在线聊天室
- AJAX中的get请求方式
- ajax中的GET 与POST
- ajax中的get和post
- Ajax 中的GET和POST
- Django中的request.GET和request.POST
- Django中的request.GET和request.POST
- Django中的request.GET和request.POST
- django+mysql中的get和filter操作
- Django中的request.GET和request.POST
- Django教程笔记:五、post get ajax异步操作
- ZooKeeper 是什么?
- MySQL索引特性笔记
- 十个利用矩阵乘法解决的经典题目
- ZOJ 2770 Burn the Linked Camp 差分约束
- Leetcode|Pow(x,n)
- django中的ajax实现(GET)
- Java 理论与实践: 正确使用 Volatile 变量
- 键盘挡住文本,调整键盘高度简单办法
- MySQL的日志管理
- hdu 1151 Air Raid
- MySQL优化一之limit优化
- php 中大括号的规范
- POJ_1679_The Unique MST(次小生成树模板)
- C语言的执行过程