将数据从HTML表传递到json,然后将其放置在xlsx文件中,但单元格中的输出将其添加到下划线顶部的我的派生数据中。输出在文件views.py中。因此,必须使输出后他给我最终文件(此功能在print(6)之后在views.py文件中起作用)并且他也不想给我下载文件。寻求帮助。
这是我在文件views.py中拥有的内容,当您按下按钮时,它会起作用
@csrf_exempt
def table(request):
if request.POST:
print(1)
shutil.copy("media/xl1.xlsx", "media/xl2.xlsx")
destination_filename = "media/xl2.xlsx"
print(2)
d1 = request.POST["d1"]
d2 = request.POST["d2"]
d3 = request.POST["d3"]
d4 = request.POST["d4"]
a = request.POST["a"]
b2 = request.POST["b2"]
b3 = request.POST["b3"]
b4 = request.POST["b4"]
c2 = request.POST["c2"]
print(3)
wb = openpyxl.load_workbook('media/xl2.xlsx')
print(4)
worksheet = wb['Лист1']
worksheet["B4"]= d1
worksheet["C4"]= d2
worksheet["D4"]= d3
worksheet["E4"]= d4
worksheet["B5"]= a
worksheet["C6"]= b2
worksheet["D6"]= b3
worksheet["E6"]= b4
worksheet["C7"]= c2
print(5)
wb.save('media/xl2.xlsx')
print(6)
def handle_uploaded_file(f):
with open('media/xl2.xlsx', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
print(7)
return render(request,'table.html', {})
这是按钮和JS代码
<a href="#" class="btn btn-success" id="test" > Вывод в Excel</a>
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function (event) {
event.preventDefault();
var d1 = document.getElementById("d1");
var d2 = document.getElementById("d2");
var d3 = document.getElementById("d3");
var d4 = document.getElementById("d4");
var a = document.getElementById("a");
var b2 = document.getElementById("b2");
var b3 = document.getElementById("b3");
var b4 = document.getElementById("b4");
var c2 = document.getElementById("c2");
console.clear();
console.log(d1.innerHTML);
console.log(d2.innerHTML);
console.log(d3.innerHTML);
console.log(d4.innerHTML);
console.log(a.innerHTML);
console.log(b2.innerHTML);
console.log(b3.innerHTML);
console.log(b4.innerHTML);
console.log(c2.innerHTML);
$.post("/table/", { "d1": d1.textContent,"d2": d2.textContent,"d3": d3.textContent,"d4": d4.textContent,"a": a.textContent,"b2": b2.textContent,"b3": b3.textContent,"b4": b4.textContent,"c2": c2.textContent}, function (response) {
});
});
});
</script>