欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django 使用Ajax進行前后臺交互的示例講解

 更新時間:2018年05月28日 10:52:33   作者:笛在月明  
今天小編就為大家分享一篇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批量生成特定尺寸圖片及圖畫任意文字的實例

    Python批量生成特定尺寸圖片及圖畫任意文字的實例

    今天小編就為大家分享一篇Python批量生成特定尺寸圖片及圖畫任意文字的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python實現(xiàn)PID算法及測試的例子

    python實現(xiàn)PID算法及測試的例子

    今天小編就為大家分享一篇python實現(xiàn)PID算法及測試的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python數(shù)組遍歷的簡單實現(xiàn)方法小結(jié)

    Python數(shù)組遍歷的簡單實現(xiàn)方法小結(jié)

    這篇文章主要介紹了Python數(shù)組遍歷的簡單實現(xiàn)方法,結(jié)合實例總結(jié)分析了Python針對數(shù)組的元素,索引常用遍歷技巧,需要的朋友可以參考下
    2016-04-04
  • python自制包并用pip免提交到pypi僅安裝到本機【推薦】

    python自制包并用pip免提交到pypi僅安裝到本機【推薦】

    這篇文章主要介紹了python自制包并用pip免提交到pypi僅安裝到本機,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Python進行密碼學反向密碼教程

    Python進行密碼學反向密碼教程

    這篇文章主要為大家介紹了Python進行密碼學反向密碼的教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Pandas時間序列重采樣(resample)方法中closed、label的作用詳解

    Pandas時間序列重采樣(resample)方法中closed、label的作用詳解

    這篇文章主要介紹了Pandas時間序列重采樣(resample)方法中closed、label的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • 如何使用python爬取csdn博客訪問量

    如何使用python爬取csdn博客訪問量

    這篇文章主要介紹了如何使用python爬取csdn博客訪問量的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Python Dataframe常見索引方式詳解

    Python Dataframe常見索引方式詳解

    這篇文章主要介紹了Python Dataframe常見索引方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Python中實現(xiàn)定時任務常見的幾種方式

    Python中實現(xiàn)定時任務常見的幾種方式

    在Python中,實現(xiàn)定時任務是一個常見的需求,無論是在自動化腳本、數(shù)據(jù)處理、系統(tǒng)監(jiān)控還是其他許多應用場景中,Python提供了多種方法來實現(xiàn)定時任務,包括使用標準庫、第三方庫以及系統(tǒng)級別的工具,本文將詳細介紹幾種常見的Python定時任務實現(xiàn)方式
    2024-08-08
  • django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式

    django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式

    這篇文章主要介紹了django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05

最新評論