Django 使用Ajax進(jìn)行前后臺(tái)交互的示例講解
本文要實(shí)現(xiàn)的功能是:根據(jù)下拉列表的選項(xiàng)將數(shù)據(jù)庫中對(duì)應(yīng)的內(nèi)容顯示在頁面,選定要排除的選項(xiàng)后,提交剩余的選項(xiàng)到數(shù)據(jù)庫。
為了方便前后臺(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ù)庫
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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python pygame實(shí)現(xiàn)2048游戲
這篇文章主要為大家詳細(xì)介紹了python pygame實(shí)現(xiàn)2048游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
使用TensorFlow對(duì)圖像進(jìn)行隨機(jī)旋轉(zhuǎn)的實(shí)現(xiàn)示例
這篇文章主要介紹了使用TensorFlow對(duì)圖像進(jìn)行隨機(jī)旋轉(zhuǎn)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
使用scrapy實(shí)現(xiàn)爬網(wǎng)站例子和實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(蜘蛛)的步驟
本文分二個(gè)示例,第一個(gè)是個(gè)簡(jiǎn)單的爬網(wǎng)站的小例子,第二個(gè)例子實(shí)現(xiàn)目是從一個(gè)網(wǎng)站的列表頁抓取文章列表,然后存入數(shù)據(jù)庫中,數(shù)據(jù)庫包括文章標(biāo)題、鏈接、時(shí)間,大家參考使用吧2014-01-01
python 字典(dict)遍歷的四種方法性能測(cè)試報(bào)告
本文主要是針對(duì)Python的字典dict遍歷的4種方法進(jìn)行了性能測(cè)試,以便分析得出效率最高的一種方法2014-06-06
python之如何使用openpyxl設(shè)置單元格樣式
這篇文章主要介紹了python之如何使用openpyxl設(shè)置單元格樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

