Django 使用Ajax進行前后臺交互的示例講解
本文要實現(xiàn)的功能是:根據(jù)下拉列表的選項將數(shù)據(jù)庫中對應的內(nèi)容顯示在頁面,選定要排除的選項后,提交剩余的選項到數(shù)據(jù)庫。
為了方便前后臺交互,利用了Ajax的GET和POST方法分別進行數(shù)據(jù)的獲取和提交。
代碼如下:
<!--利用獲取的數(shù)據(jù)進行表單內(nèi)容的填充--> <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>
<!--選擇并提交數(shù)據(jù)--> <script> //選擇數(shù)據(jù) function postselect (){ var seleitem=new Array(); $("input[name='chk']").each(function(i){ if(!($(this).is( ":checked" )) ){ seleitem[i]=$(this).val(); // alert(seleitem[i]); } }); //將排除后的數(shù)據(jù)提交到后臺數(shù)據(jù)庫 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進行前后臺交互的示例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)組遍歷的簡單實現(xiàn)方法小結(jié)
這篇文章主要介紹了Python數(shù)組遍歷的簡單實現(xiàn)方法,結(jié)合實例總結(jié)分析了Python針對數(shù)組的元素,索引常用遍歷技巧,需要的朋友可以參考下2016-04-04python自制包并用pip免提交到pypi僅安裝到本機【推薦】
這篇文章主要介紹了python自制包并用pip免提交到pypi僅安裝到本機,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06Pandas時間序列重采樣(resample)方法中closed、label的作用詳解
這篇文章主要介紹了Pandas時間序列重采樣(resample)方法中closed、label的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式
這篇文章主要介紹了django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05