欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-01-29 09:32:57|栏目: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)

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

上一篇:python pygame实现球球大作战

栏    目:Python代码

下一篇:python中如何正确使用正则表达式的详细模式(Verbose mode expression)

本文标题:基于django传递数据到后端的例子

本文地址:http://www.codeinn.net/misctech/53384.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有