基于django传递数据到后端的例子

yipeiwu_com6年前Python基础

最近遇到一个问题,前端表单我写了多个按钮,每个按钮通过for循环来给name赋值如下:

<input type="button" class="btn btn-info btn-xs" name="{{item.document}}" value="解析" οnclick="Parsefunc(this.name)">

问题是我想要实现点击哪个按钮就传对应按钮的值到后端,对于我这样的前端新手就比较麻烦了。。。于是乎,各种询问、谷歌...用了三天才发现原来实现出来那么简单,要被大神们嘲笑了,废话少说,我用了ajax传递数据:

function Parsefunc(dataname){
// var dataname = $(this).attr('name');
// alert(dataname);
 $.ajax({
 url:"/file_parse/",
 type:"POST",
 contentType: "application/json",
 data:JSON.stringify({
 'data':dataname
 }), 
 success:function(response){
 window.wxc.xcConfirm("成功", window.wxc.xcConfirm.typeEnum.success);
 },
  error:function(response){
  window.wxc.xcConfirm("失败", window.wxc.xcConfirm.typeEnum.error);
  }
 })
 }

在后端用了rest_framework

from rest_framework.decorators import api_view
 
@api_view(['GET', 'POST'])
def file_parse(request):
 uploadfile_info = upload_document.objects.all()
 if request.method == 'POST':
  info = request.data.get('data')
  inf = request.data
  print(info)
  print(inf)
context = {'uploadfile_info': uploadfile_info}
 return render(request, 'logfile/file_parse.html', context)

成功,至少这个值是打印出来了,功能实现了,毕竟实现第一,改进第二,还得得慢慢磨练,在此分享也希望大家不吝赐教

以上这篇基于django传递数据到后端的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

numpy.ndarray 实现对特定行或列取值

numpy.ndarray 实现对特定行或列取值

如下所示: import numpy as np b = [[1,2,0], [4,5,0], [7,8,1], [4,0,1], [7,11,1] ] a=np.array...

Python对象体系深入分析

Python对象体系深入分析

本文较为详细的分析了了Python的对象体系。分享给大家供大家参考。具体如下: Guido用C语言创造了Python,在Python的世界中一切皆为对象. 一.C视角中的Python对象...

详解K-means算法在Python中的实现

详解K-means算法在Python中的实现

K-means算法简介 K-means是机器学习中一个比较常用的算法,属于无监督学习算法,其常被用于数据的聚类,只需为它指定簇的数量即可自动将数据聚合到多类中,相同簇中的数据相似度较高...

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...

Numpy array数据的增、删、改、查实例

准备工作: 增、删、改、查的方法有很多很多种,这里只展示出常用的几种。 >>> import numpy as np >>> a = np.ar...