基于django传递数据到后端的示例分析

这篇文章主要介绍了基于django传递数据到后端的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

最近遇到一个问题,前端表单我写了多个按钮,每个按钮通过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传递数据到后端的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,更多相关知识等着你来学习!