这次给大家带来django中的ajax使用方法,django中ajax使用的注意事项有哪些,下面就是实战案例,一起来看一下。
django 是由 python 开发的一个免费的开源网站框架,可以用于快速搭建高性能,优雅的网站!
ajax = asynchronous javascript and xml(异步的 javascript 和 xml)。
ajax 不是新的编程语言,而是一种使用现有标准的新方法。
ajax 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。
ajax
很多时候,我们在网页上请求操作时,不需要刷新页面。实现这种功能的技术就要ajax!
jquery中的ajax就可以实现不刷新页面就能向后台请求或提交数据的功能,我们仍然用它来做django中的ajax,所以先把jquey下载下来,版本越高越好。
一、ajax发送简单数据类型:
html代码:在这里我们仅发送一个简单的字符串
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-">
<title></title>
</head>
<body>
<input type="button" onclick="ajaxsubmit();" value="提交">
<script src="/static/jquery-...min.js"></script>
<script>
function ajaxsubmit(){
var host = '...';
var port = '';
$.ajax({
url:/app/ajax_submit/,
type:'post',
data:{host:host,port:port},
success: function (arg) {
}
});
}
</script>
</body>
</html>
django下app里views.py
# coding:utf-8
from django.shortcuts import render,httpresponse
def ajax_submit(request):
print request.post #客户端发来的数据
return render(request,'ajax_submit.html')
打印出来的数据样式:
二、ajax发送复杂的数据类型:
html代码:在这里我们仅发送一个列表中包含字典数据类型
由于发送的数据类型为列表 字典的格式,我们提前要把它们转换成字符串形式,否则后台程序接收到的数据格式不是我们想要的类型,所以在ajax传输数据时需要json
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-">
<title></title>
</head>
<body>
<input type="button" onclick="ajaxsubmit_set();" value="提交集合">
<script src="/static/jquery-...min.js"></script>
<script>
function ajaxsubmit_set(){
var data_list = [
{'name':'chenchao','age':},
{'name':'lisi','age':},
{'name':'wangwu','age':}
];
$.ajax({
url:/app/ajax_submit_set/,
type:'post',
tradition:true, 原生模式
data:{data:json.stringify(data_list)},
success: function (arg) {
}
});
}
</script>
</body>
</html>
django下app里views.py
def ajax_submit_set(request):
print request.post
return render(request,'ajax_submit.html')
打印出来的数据样式:
三、稍等、还没完。
虽然我们实现了功能,但这还不够,因为显得不是很专业,所以我们稍作处理。
success: function (arg) { } 如果ajax提交数据成功,那么就会自动执行这里面的函数
html代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-">
<title></title>
</head>
<body>
<input type="button" onclick="ajaxsubmit();" value="提交">
<input type="button" onclick="ajaxsubmit_set();" value="提交集合">
<script src="/static/jquery-...min.js"></script>
<script>
function ajaxsubmit(){
var host = '...';
var port = '';
$.ajax({
url:/app/ajax_submit/,
type:'post',
data:{host:host,port:port},
success: function (arg) {
}
});
}
function ajaxsubmit_set(){
var data_list = [
{'name':'chenchao','age':},
{'name':'lisi','age':},
{'name':'wangwu','age':}
];
$.ajax({
url:/app/ajax_submit_set/,
type:'post',
tradition:true,
data:{data:json.stringify(data_list)},
success: function (arg) { //如果程序执行成功就会执行这里的函数
var callback_dic = $.parsejson(arg);
if(callback_dic.status){
alert('成功');
}else{
alert(callback_dic.error); //把错误的信息从后台提出展示出来
}
}
});
}
</script>
</body>
</html>
django下app里views.py
# coding:utf-
from django.shortcuts import render,httpresponse,redirect
def ajax_submit(request):
print request.post
return render(request,'ajax_submit.html')
import json
def ajax_submit_set(request):
ret = {'status': true,'error': }
try:
print request.pos
except exception, e:
ret['status'] = false
ret['error'] = str(e)
j_ret = json.dumps(ret)
return httpresponse(j_ret)
django中ajax的使用
前端的ajax代码如下所示:
$.ajax({
type:'get',
url:'/store/ds_mgmt_wx/ajax_handle',
datatype:'html',
success:function(data)
{
alert(data);
},
error:function(data)
{
alert(data);
}
});
后端的相应代码的返回方法如下:
if act_job == 'ajax_handle':
return httpresponse('ajax_handle')
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
详解ajax的原理及优缺点有哪些
ajax与iframe框架实现图片文件上传(图文详解)
以上就是django中的ajax使用方法的详细内容。