0

views.py

def export(request):
    print('start')
    ourid = request.POST.getlist("terid")
    queryset = Case_Info.objects.filter(id__in=list(map(int, ourid)))
    Case_Detail = Case_Info_Resource()
    print(ourid)
    dataset = Case_Detail.export(queryset)  # error in this line
    response = HttpResponse(
        dataset.xls, content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="persons.xls"'
    print('end')
    return response

Ajax Script

$(document).ready(function () {
    $('#Download').click(function () {
        console.log("clicked!")
        var list = [];
        $("input:checkbox[name='checkbox']:checked").each(function () {
            list.push($(this).val());
        });
        $('#Download').attr('disabled', 'disabled');
        $.ajax({
            url: '/Account_Manager/Download/',
            type: 'POST',
            data: {
                'terid': list,
                'csrfmiddlewaretoken': '{{csrf_token}}',
            },
            timeout: 30000,
            traditional: true,
            dataType: 'text',
            success: function () {
                alert("The best cricketers are: " + list.join(", "));
                $('#Download').removeAttr('disabled');
            }
        });
    });
});

What I am trying to do is pass several ids from the front end to the back and then export data from the database accordingly. everything is working fine till this following line.

dataset = Case_Detail.export(queryset)

after this line, it again reaches to the beginning of the function that results in the blank list that results in an empty excel file

Output

  • 1
    So the export function appears to be called twice (two 'starts' outputs). What is calling it twice? Can you remove ajax and call the view on its own? In the second call, there is no 'ourid' value. Is this because it's being called as a GET not a POST? – Matthew Hegarty Aug 11 '20 at 11:40
  • @MatthewHegarty you write that second time it's being called as GET. thanks for adding that. view on its own working fine. but I need ajax to pass selected checkboxes. so that I can download only selected data. – Ashutosh Gupta Aug 11 '20 at 12:01
  • it should work ok, you just need to figure out why Ajax is making the call twice. Could it be a double-click? could be refreshing the page somewhere? – Matthew Hegarty Aug 11 '20 at 12:15
  • @MatthewHegarty , thanx man, my jquery is not properly included. So now its calling only once. but now it not downloading the excel. – Ashutosh Gupta Aug 11 '20 at 12:50
  • @MatthewHegarty I can see the response data in developer tab but the file is not getting downloaded. – Ashutosh Gupta Aug 11 '20 at 12:59
  • probably best to create a new question for that – Matthew Hegarty Aug 11 '20 at 13:17
  • @MatthewHegarty I don't know but it is downloading blank excel as soon as I remove jquery. from base file – Ashutosh Gupta Aug 11 '20 at 14:04

1 Answers1

0

So, finally, I have achieved what I want.

I want to pass selected id's (multiple ids) from the frontend to the backend, and then fetch data from the database accordingly. After that, I want to export the data into excel or CSV format.

Ajax:

<script>

    $(document).ready(function (e) {
        $('#Download').click(function (e) {
            e.preventDefault()
            console.log("clicked!")
            var list = [];
            $("input:checkbox[name='checkbox']:checked").each(function () {
                list.push($(this).val());
            });
            $.ajax({
                url: '/Account_Manager/Download/',
                type: 'POST',
                data: {
                    'terid': list,
                    'csrfmiddlewaretoken': '{{csrf_token}}',
                },
                traditional: true,
                dataType: 'text',
                success: function (response, status, xhr) {
                    var filename = "persons.csv";
                    var disposition = xhr.getResponseHeader('Content-Disposition');
                    if (disposition && disposition.indexOf('attachment') !== -1) {
                        var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                        var matches = filenameRegex.exec(disposition);
                        if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                    }

                    var type = xhr.getResponseHeader('Content-Type');
                    var blob = new Blob([response], {type: type});

                    if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
                        window.navigator.msSaveBlob(blob, filename);
                    } else {
                        var URL = window.URL || window.webkitURL;
                        var downloadUrl = URL.createObjectURL(blob);

                        if (filename) {
                            // use HTML5 a[download] attribute to specify filename
                            var a = document.createElement("a");
                            // safari doesn't support this yet
                            if (typeof a.download === 'undefined') {
                                window.location.href = downloadUrl;
                            } else {
                                a.href = downloadUrl;
                                a.download = filename;
                                document.body.appendChild(a);
                                a.click();
                            }
                        } else {
                            window.location.href = downloadUrl;
                        }

                        setTimeout(function () {
                            URL.revokeObjectURL(downloadUrl);
                        }, 100); // cleanup
                    }
                }
            });

        });
    });
</script>

Person.csv is the file I am passing through views.py

View.py

def export(request):
    ourid = request.POST.getlist("terid")
    queryset = Case_Info.objects.filter(id__in=list(map(int, ourid)))
    dataset = Case_Info_Resource().export(queryset)
    response = HttpResponse(dataset.csv, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="persons.csv"'
    return response

If your dataset replays an empty list please check the console of developer tab for error in js file you included in the document, and make sure that you do not include any js file twice in the same file.

Thanks to everybody who helped me

Matthew Hegarty
  • 3,791
  • 2
  • 27
  • 42