Django 使用Ajax进行前后台交互的示例讲解
作者:笛在月明 时间:2023-08-03 03:57:47
本文要实现的功能是:根据下拉列表的选项将数据库中对应的内容显示在页面,选定要排除的选项后,提交剩余的选项到数据库。
为了方便前后台交互,利用了Ajax的GET和POST方法分别进行数据的获取和提交。
代码如下:
<!--利用获取的数据进行表单内容的填充-->
<script>
$("#soft_id").change(function(){
var softtype=$("#soft_id").find("option:selected").text();
var soft={'type_id':softtype}
$.ajax( {
type: 'GET',
url:'/data/soft-filter/{{family}}',
dataType: 'json',
data:soft,
success: function( data_get ){
build_dropdown( data_get, $( '#min_version' ), '请选择最低版本' );//填充表单
build_dropdown( data_get, $( '#max_version' ), '请选择最高版本' );
build_div(data_get,$('#soft_affected'));
}
});
});
var build_dropdown = function( data, element, defaultText ){
element.empty().append( '<option value="">' + defaultText + '</option>' );
if( data ){
$.each( data, function( key, value ){
element.append( '<option value="' + key + '">' + value + '</option>' );
} );
}
}
var build_div = function( data, element){
if( data ){
element.empty();
$.each( data, function( key, value ){
element.append(' <li class="clearfix"> <div class="todo-check pull-left"><input name="chk" type="checkbox" value="'+value+'" /></div> <div class="todo-title">'+value+' </div><div class="todo-actions pull-right clearfix"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-complete"><i class="fa fa-check"></i></a><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-edit"><i class="fa fa-edit"></i></a><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="todo-remove"><i class="fa fa-trash-o"></i></a></div> </li>');
} );
}
}
</script>
<!--选择并提交数据-->
<script>
//选择数据
function postselect (){
var seleitem=new Array();
$("input[name='chk']").each(function(i){
if(!($(this).is( ":checked" )) ){
seleitem[i]=$(this).val();
// alert(seleitem[i]);
}
});
//将排除后的数据提交到后台数据库
var soft={'type_id':seleitem}
$.ajax( {
type: 'POST',
url:'/data/soft-submit',
dataType: 'json',
data:soft,
success: function( data_get ){
}
});
}
</script>
部分html代码为:
<div style="overflow: hidden;" >
<ul id='soft_affected' class="todo-list sortable">
</ul>
</div>
views.py中处理请求和响应代码:
def soft_submit(request):
if request.is_ajax():
id=request.POST.get('type_id')
return HttpResponse("success")
def soft_filter(request,fami):
softtype=''
ajax_release_version=[]
release_version=[]
if request.is_ajax():
softtype=request.GET.get('type_id')
soft_type=SoftTypeRef.objects.using('vul').filter(description=softtype)
soft_tp_id=0
for i in soft_type:
soft_tp_id= i.soft_type_id
web_soft=SoftWeb.objects.using('vul').filter(soft_type_id=soft_tp_id)
for i in web_soft:
ajax_release_ver=i.release_version
ajax_release_version.append(ajax_release_ver)
return HttpResponse(json.dumps(ajax_release_version), content_type='application/json')
来源:https://blog.csdn.net/IqqIqqIqqIqq/article/details/52160662
标签:Django,Ajax,交互
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Linux下利用python实现语音识别详细教程
2022-07-13 15:11:55
![](https://img.aspxhome.com/file/2023/7/65377_0s.png)
django-rest-swagger对API接口注释的方法
2022-04-12 00:06:02
![](https://img.aspxhome.com/file/2023/6/100796_0s.jpg)
让apache2以cgi方式运行perl cgi程序的实现方法
2023-08-15 18:02:57
在ubuntu16.04中将python3设置为默认的命令写法
2022-06-21 10:12:41
![](https://img.aspxhome.com/file/2023/9/111959_0s.png)
Nodejs做文本数据处理实现详解
2024-05-13 10:04:06
python爬虫之urllib,伪装,超时设置,异常处理的方法
2022-07-23 23:47:10
Numpy随机抽样的实现
2022-06-24 07:18:02
Python使用multiprocessing实现一个最简单的分布式作业调度系统
2022-06-14 07:43:33
Mysql5.7.11在windows10上的安装与配置(解压版)
2024-01-13 18:07:49
php+jQuery实现的三 级导航栏下拉菜单显示效果
2023-11-14 12:32:05
![](https://img.aspxhome.com/file/2023/1/55601_0s.jpg)
python适合做数据挖掘吗
2021-03-25 00:42:58
pytorch中DataLoader()过程中遇到的一些问题
2022-01-17 18:36:11
php获取当前页面完整URL地址
2024-05-13 09:20:56
解决python中set与dict的无序问题
2021-02-24 14:50:31
![](https://img.aspxhome.com/file/2023/7/105287_0s.jpg)
月影:JavaScript王者归来
2008-08-05 17:18:00
![](https://img.aspxhome.com/file/UploadPic/20088/5/200885172229312s.jpg)
mysql优化之慢查询分析+explain命令分析+优化技巧总结
2024-01-16 03:12:30
![](https://img.aspxhome.com/file/2023/2/109792_0s.png)
python修改操作系统时间的方法
2022-10-14 14:13:29
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
2021-10-03 08:39:35
![](https://img.aspxhome.com/file/2023/7/127657_0s.png)
如何使用Django Admin管理后台导入CSV
2022-12-28 19:56:52
![](https://img.aspxhome.com/file/2023/5/69635_0s.png)
JavaScript中你不知道的Object.entries用法
2024-04-19 11:01:40