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

yipeiwu_com5年前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设计】。

相关文章

Python中的测试模块unittest和doctest的使用教程

我要坦白一点。尽管我是一个应用相当广泛的公共域 Python 库的创造者,但在我的模块中引入的单元测试是非常不系统的。实际上,那些测试大部分 是包括在 gnosis.xml.pickle...

Python 字符串转换为整形和浮点类型的方法

Python2.6 之前:字符串转换为整形和浮点型 >>>import string >>>string.atoi('34.1') 34 &...

Python 实现文件打包、上传与校验的方法

不多说,我们直接上源码: # -*- coding:UTF-8 -*- ''' 实现文件打包、上传与校验 Created on 2018年1月12日 @author: liuyazh...

Python 窗体(tkinter)按钮 位置实例

如下所示: import tkinter def go(): #函数 print("go函数") win=tkinter.Tk() #构造窗体 win.title("he...

跟老齐学Python之有容乃大的list(1)

前面的学习中,我们已经知道了两种python的数据类型:int和str。再强调一下对数据类型的理解,这个世界是由数据组成的,数据可能是数字(注意,别搞混了,数字和数据是有区别的),也可能...