基于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设计】。

相关文章

python BlockingScheduler定时任务及其他方式的实现

本文介绍了python BlockingScheduler定时任务及其他方式的实现,具体如下: #BlockingScheduler定时任务 from apscheduler.sc...

Python 中的 else详解

我们都知道 Python 中else的基本用法是在条件控制语句中的 if...elif...else...,但是 else 还有两个其它的用途,一是用于循环的结尾,另一个是用在错误处理的...

详解Python 2.6 升级至 Python 2.7 的实践心得

前言 CentOS 6.8 安装 Python 2.7.13,因为软件版本上的需求所以考虑将 Python 升级至 2.7.13,加上生产环境还是以 RHEL 6 为主,互联网自动化运...

全面了解Python的getattr(),setattr(),delattr(),hasattr()

1. getattr()函数是Python自省的核心函数,具体使用大体如下: class A: def __init__(self): self.name = 'zhangji...

Python实现字符串逆序输出功能示例

本文实例讲述了Python实现字符串逆序输出功能。分享给大家供大家参考,具体如下: 1、有时候我们可能想让字符串倒序输出,下面给出几种方法 方法一:通过索引的方法 >>&...