首页 > 编程 > Python > 正文

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

2019-11-25 11:57:39
字体:
来源:转载
供稿:网友

最近遇到一个问题,前端表单我写了多个按钮,每个按钮通过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传递数据到后端的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表