动态表单加载程序(Django)

时间:2019-06-04 04:43:43

标签: javascript jquery django python-3.x

views.py

我具有以下功能(用于excel下载):

def download(request):
    testname = request.POST.get('testname')
    lis_final=[]
    for i in range(0,10):
        lis = []                    
        lis.insert(0,testname)
        ''' like this i had inserted 30 rows '''
        lis.insert(29,testname)
        lis_final.append(lis)
    rb = xlrd.open_workbook(os.path.join(settings.MEDIA_ROOT, 'sample.xls'),formatting_info=True)
    r_sheet = rb.sheet_by_index(0)
    r = r_sheet.nrows
    wb = copy(rb)
    sheet = wb.get_sheet(0)
    rows_insert = len(lis_final)
    for i in range(3,r+rows_insert):
        for j in range(0,30):
            sheet.write(i,j,lis_final[i-3][j])
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="test.xls"'
    wb.save(response)
    return response

index.html

我有以下锚标签可供下载:

<div class="col-2"> 
    <a href="javascript:void(0)" id="excel_dwnld"><i class="fa fa-file-excel-o"></i> Download</a>                   
</div>

我正在js中将此函数称为文件下载方式:

$(document).on("click","#excel_dwnld",function(){
    var testname=$("#id_testname").val();
    var form = document.createElement("form");
    var element1 = document.createElement("input");
    form.method = "POST";
    form.action= "{% url 'test:download' %}";//function call
    element1.value=testname;
    element1.name="testname";
    element1.setAttribute("type", "hidden");
    form.appendChild(element1);
    document.body.appendChild(form);
    //$("#loader-div").show(); // this is my loader
    form.submit();  
});

根据我的要求,应创建动态表格,并且该表格应提交以供Excel下载。

在提交表单时,我想显示加载程序。提交并下载文件后,该加载器应隐藏。由于这是文件响应,因此我的加载器没有隐藏。请帮帮我。

谢谢。

0 个答案:

没有答案