Django 使用Ajax进行前后台交互的示例讲解
2018-05-28 10:52
791 查看
本文要实现的功能是:根据下拉列表的选项将数据库中对应的内容显示在页面,选定要排除的选项后,提交剩余的选项到数据库。
为了方便前后台交互,利用了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')
以上这篇Django 使用Ajax进行前后台交互的示例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Django中使用Ajax进行前后台交互
- jQuery.ajax与后台服务交互的几种类型使用示例
- Django中使用jquery的ajax进行数据交互的实例代码
- 使用Ajax提交数据,进行前后台数据交互
- vue使用ajax获取后台数据进行显示的示例
- AngularJS实现与后台服务器进行交互的示例讲解
- 使用Bootstrap的popover标签中嵌入插件,并且为插件注册事件实现Ajax与后台交互
- 在非英文字符集的页面上,如果使用Ajax方式进行数据交互的话,就必须要注意保证前后端数据的统一编码,否则,很容易就出现乱码!
- ThinkPHP中使用ajaxReturn进行ajax交互
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- Jquery 中 ajaxSubmit使用讲解 j-m 后台登录
- THINKPHP中使用$this->ajaxReturn与模板页进行AJAX交互使用方法
- 关于ASP.NET 中使用Ajax进行异步调用问题,前台参数无法跳转到后台,提示500 internal server error
- 使用AJAX与后台交互局部数据
- 【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
- 实现可编辑单元格(使用Ajax完成后台交互)。
- 跳ajax方式进行前后台交互之后台代码要怎么写
- 使用ajax局部刷新gridview进行数据绑定示例
- Spring3 MVC 使用JSON进行前后台数据交互
- .NetMVC与EXT4使用json进行前后台交互