基于django傳遞數(shù)據(jù)到后端的例子
最近遇到一個(gè)問題,前端表單我寫了多個(gè)按鈕,每個(gè)按鈕通過for循環(huán)來給name賦值如下:
<input type="button" class="btn btn-info btn-xs" name="{{item.document}}" value="解析" οnclick="Parsefunc(this.name)">
問題是我想要實(shí)現(xiàn)點(diǎn)擊哪個(gè)按鈕就傳對應(yīng)按鈕的值到后端,對于我這樣的前端新手就比較麻煩了。。。于是乎,各種詢問、谷歌...用了三天才發(fā)現(xiàn)原來實(shí)現(xiàn)出來那么簡單,要被大神們嘲笑了,廢話少說,我用了ajax傳遞數(shù)據(jù):
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)
成功,至少這個(gè)值是打印出來了,功能實(shí)現(xiàn)了,畢竟實(shí)現(xiàn)第一,改進(jìn)第二,還得得慢慢磨練,在此分享也希望大家不吝賜教
以上這篇基于django傳遞數(shù)據(jù)到后端的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)pdf轉(zhuǎn)換成word/txt純文本文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)pdf轉(zhuǎn)換成word和txt純文本文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
python虛擬環(huán)境virtualenv的安裝與使用
virtualenv用于創(chuàng)建獨(dú)立的Python環(huán)境,多個(gè)Python相互獨(dú)立,互不影響,它能夠:1. 在沒有權(quán)限的情況下安裝新套件 2. 不同應(yīng)用可以使用不同的套件版本 3. 套件升級不影響其他應(yīng)用2017-09-09
python實(shí)現(xiàn)按行切分文本文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)按行切分文本文件的方法,涉及Python利用shell命令操作文本文件的相關(guān)技巧,需要的朋友可以參考下2016-04-04

