[django]从前端返回字符串,后端转换为字典,执行数据添加操作
具体如题:
js代码如下:
$('#bill_add').click(function(){//合同添加 var bill1 = $("#bill1").val();var bill2 = $("#bill2").val();var bill3 = $("#bill3").val();var bill4 = $("#bill4").val();var bill5 = $("#bill5").val(); var bill6 = $("#bill6").val();var bill7 = $("#bill7").val();var bill8 = $("#bill8").val();var bill9 = $("#bill9").val();var bill10 = $("#bill10").val(); var bill11 = $("#bill1").val();var bill12 = $("#bill12").val();var bill13 = $("#bill13").val();var bill14 = $("#bill14").val();var bill15 = $("#bill15").val(); var bill16 = $("#bill16").val();var bill17 = $("#bill17").val();var bill18 = $("#bill18").val();var bill19 = $("#bill19").val();var bill20 = $("#bill20").val(); var bill21 = $("#bill21").val();var bill22 = $("#bill22").val();var bill23 = $("#bill23").val();var bill24 = $("#bill24").val();var bill25 = $("#bill25").val(); var bill26 = $("#bill26").val();var bill27 = $("#bill27").val();var bill28 = $("#bill28").val();var bill29 = $("#bill29").val();var bill30 = $("#bill30").val(); var bill31 = $("#bill31").val();var bill32 = $("#bill32").val();var bill33 = $("#bill33").val();var bill34 = $("#bill34").val();var bill35 = $("#bill35").val(); var bill36 = $("#bill36").val();var bill37 = $("#bill37").val();var bill38 = $("#bill38").val();var bill39 = $("#bill39").val();var bill40 = $("#bill40").val(); var bill41 = $("#bill41").val(); $.post("{% url 'add_contract' %}", { csrfmiddlewaretoken:"{{ csrf_token }}", bill_str:"{'bill1':'"+bill1+"', 'bill2':'"+bill2+"', 'bill3':'"+bill3+"', 'bill4':'"+bill4+"', 'bill5':'"+bill5+"'," +"'bill6':'"+bill6+"', 'bill7':'"+bill7+"', 'bill8':'"+bill8+"', 'bill9':'"+bill9+"', 'bill10':'"+bill10+"'," +"'bill11':'"+bill11+"', 'bill12':'"+bill12+"', 'bill13':'"+bill13+"', 'bill14':'"+bill14+"', 'bill15':'"+bill15+"'," +"'bill16':'"+bill16+"', 'bill17':'"+bill17+"', 'bill18':'"+bill18+"', 'bill19':'"+bill19+"', 'bill20':'"+bill20+"'," +"'bill21':'"+bill21+"', 'bill22':'"+bill22+"', 'bill23':'"+bill23+"', 'bill24':'"+bill24+"', 'bill25':'"+bill25+"'," +"'bill26':'"+bill26+"', 'bill27':'"+bill27+"', 'bill28':'"+bill28+"', 'bill29':'"+bill29+"', 'bill30':'"+bill30+"'," +"'bill31':'"+bill31+"', 'bill32':'"+bill32+"', 'bill33':'"+bill33+"', 'bill34':'"+bill34+"', 'bill35':'"+bill35+"'," +"'bill36':'"+bill36+"', 'bill37':'"+bill37+"', 'bill38':'"+bill38+"', 'bill39':'"+bill39+"', 'bill40':'"+bill40+"'," +"'bill41':'"+bill41+"'}", }, function(data,status) { ; $('#test').html(data['bill40']); } ) });
view中代码:
def add_contract(request): bill_str = (request.POST['bill_str']) bill_dic = eval(bill_str) Bill.objects.create(contract_code=bill_dic['bill1'],contract_name=bill_dic['bill2'],new_flag=bill_dic['bill3'],original_code=bill_dic['bill4'], con_type=bill_dic['bill5'],rep_nbr=bill_dic['bill6'],acc_code=bill_dic['bill7'],pstn_count=bill_dic['bill8'], pstn_cost=bill_dic['bill9'],remark_one=bill_dic['bill10'],fibeione=bill_dic['bill11'],fibeione_cost=bill_dic['bill12'], remark_two=bill_dic['bill13'],fibeitwo=bill_dic['bill14'],fibeitwo_cost=bill_dic['bill15'],remark_three=bill_dic['bill16'], cmda_count=bill_dic['bill17'],cmda_cost=bill_dic['bill18'],remark_four=bill_dic['bill19'],qtsl_one=bill_dic['bill20'], qtfy_one=bill_dic['bill21'],remark_five=bill_dic['bill22'],qtsl_two=bill_dic['bill23'],qtfy_two=bill_dic['bill24'], remark_six=bill_dic['bill25'],qtsl_thr=bill_dic['bill26'],qtfy_thr=bill_dic['bill27'],remark_sev=bill_dic['bill28'], qtsl_fou=bill_dic['bill29'],qtfy_fou=bill_dic['bill30'],remark_egh=bill_dic['bill31'],old_charge=bill_dic['bill32'], charge=bill_dic['bill33'],deal_type=bill_dic['bill34'],con_age=bill_dic['bill35'],con_eff_date=bill_dic['bill36'], con_exp_date=bill_dic['bill37'],con_sign_date=bill_dic['bill38'],con_agent=bill_dic['bill39'],charge_type=bill_dic['bill40'], payment=bill_dic['bill41']) #bill_data = Bill.objects.all return JsonResponse(bill_dic)
实际上文章采用的是从前端返回字典的字符串形式,例如"{'a':'a'}",然后在python中利用eval函数将其转换为字典形式,然后赋值执行django模型添加数据函数
这样可以不用后端request.POST一大堆数据,例如我上面bill1到bill41岂不是要request.POST40次,这样太麻烦了!
study just for life!