Django 使用Ajax進(jìn)行前后臺(tái)交互的示例講解
本文要實(shí)現(xiàn)的功能是:根據(jù)下拉列表的選項(xiàng)將數(shù)據(jù)庫(kù)中對(duì)應(yīng)的內(nèi)容顯示在頁(yè)面,選定要排除的選項(xiàng)后,提交剩余的選項(xiàng)到數(shù)據(jù)庫(kù)。
為了方便前后臺(tái)交互,利用了Ajax的GET和POST方法分別進(jìn)行數(shù)據(jù)的獲取和提交。
代碼如下:
<!--利用獲取的數(shù)據(jù)進(jìn)行表單內(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' ), '請(qǐng)選擇最低版本' );//填充表單 build_dropdown( data_get, $( '#max_version' ), '請(qǐng)選擇最高版本' ); 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ù)提交到后臺(tái)數(shù)據(jù)庫(kù) 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中處理請(qǐng)求和響應(yīng)代碼:
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進(jìn)行前后臺(tái)交互的示例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Django使用AJAX調(diào)用自己寫(xiě)的API接口的方法
- Django中使用jquery的ajax進(jìn)行數(shù)據(jù)交互的實(shí)例代碼
- 使用Python的Django框架結(jié)合jQuery實(shí)現(xiàn)AJAX購(gòu)物車頁(yè)面
- django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
- django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)
- Django框架如何使用ajax的post方法
- Django環(huán)境下使用Ajax的操作代碼
相關(guān)文章
Python批量生成特定尺寸圖片及圖畫(huà)任意文字的實(shí)例
今天小編就為大家分享一篇Python批量生成特定尺寸圖片及圖畫(huà)任意文字的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01python實(shí)現(xiàn)PID算法及測(cè)試的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)PID算法及測(cè)試的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python數(shù)組遍歷的簡(jiǎn)單實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了Python數(shù)組遍歷的簡(jiǎn)單實(shí)現(xiàn)方法,結(jié)合實(shí)例總結(jié)分析了Python針對(duì)數(shù)組的元素,索引常用遍歷技巧,需要的朋友可以參考下2016-04-04python自制包并用pip免提交到pypi僅安裝到本機(jī)【推薦】
這篇文章主要介紹了python自制包并用pip免提交到pypi僅安裝到本機(jī),本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Pandas時(shí)間序列重采樣(resample)方法中closed、label的作用詳解
這篇文章主要介紹了Pandas時(shí)間序列重采樣(resample)方法中closed、label的作用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python Dataframe常見(jiàn)索引方式詳解
這篇文章主要介紹了Python Dataframe常見(jiàn)索引方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python中實(shí)現(xiàn)定時(shí)任務(wù)常見(jiàn)的幾種方式
在Python中,實(shí)現(xiàn)定時(shí)任務(wù)是一個(gè)常見(jiàn)的需求,無(wú)論是在自動(dòng)化腳本、數(shù)據(jù)處理、系統(tǒng)監(jiān)控還是其他許多應(yīng)用場(chǎng)景中,Python提供了多種方法來(lái)實(shí)現(xiàn)定時(shí)任務(wù),包括使用標(biāo)準(zhǔn)庫(kù)、第三方庫(kù)以及系統(tǒng)級(jí)別的工具,本文將詳細(xì)介紹幾種常見(jiàn)的Python定時(shí)任務(wù)實(shí)現(xiàn)方式2024-08-08django 連接數(shù)據(jù)庫(kù)出現(xiàn)1045錯(cuò)誤的解決方式
這篇文章主要介紹了django 連接數(shù)據(jù)庫(kù)出現(xiàn)1045錯(cuò)誤的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05