0

View.py

def export(request):
    if request.is_ajax():
        ourid = request.GET.getlist("terid")
        Case_Detail = Case_Info_Resource()
        for i in ourid:
            print(i)
            queryset = Case_Info.objects.filter(id=i)
            dataset = Case_Detail.export(queryset)
        response = HttpResponse(
            dataset.xls, content_type='application/vnd.ms-excel')
        response['Content-Disposition'] = 'attachment; filename="persons.xls"'
        print("breakpoint")
        return response

Ajax Script

<script>
                    $(document).ready(function () {
                        $('#Download').click(function () {
                            var list = [];
                            $("input:checkbox[name='checkbox']:checked").each(function () {
                                list.push($(this).val());
                            });

                            $.ajax({
                                url: '/Account_Manager/Download/',
                                type: 'GET',
                                data: { 'terid': list },
                                traditional: true,
                                dataType: 'html',
                                success: function () {
                                    alert("The best cricketers are: " + list.join(", "));
                                }
                            });
                        });
                    });
                </script>

Error: The view Apps.views.export didn't return an HttpResponse object. It returned None instead.

So, Ajax wants us to return the value in HttpResponse but I need to pass the response normally in order to download the excel which I am creating. I think I checked all the possible answers and struggling with it from the last 3 days. Thank you in advance for any help, suggestions, or edit.

  • I suggest to break it down into smaller components. 1. get the export working, 2. get the export working when called in a view via http, 3. get it working when called via AJAX. Also, using a debugger will help you much more than print statements. – Matthew Hegarty Jul 31 '20 at 07:58
  • my whole thing is working except the last return part.I am sure it works perfect till it fetching data from database using id.@MatthewHegarty – Ashutosh Gupta Jul 31 '20 at 09:15
  • There's not enough detail here to offer help. Please refer to this [guidance](https://stackoverflow.com/help/how-to-ask). Use breakpoints to step through the code and understand what is not working. – Matthew Hegarty Jul 31 '20 at 13:13
  • I finally able to achieve what I asked for this is the solution [here](https://stackoverflow.com/questions/63356184/trying-to-export-data-from-database-to-excel-in-django). – Ashutosh Gupta Aug 18 '20 at 03:04
  • I have solved it here is the answer:https://stackoverflow.com/questions/63356184/trying-to-export-data-from-database-to-excel-in-django – Ashutosh Gupta Sep 08 '20 at 06:09

0 Answers0