使用Ajax传递多个值并获取视图中的数据,以便在Django中将其导出到excel中

2024-10-01 15:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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>
                    $(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>

错误: view Apps.views.export未返回HttpResponse对象。它没有返回任何结果

因此,Ajax希望我们在HttpResponse中返回值,但我需要正常传递响应,以便下载我正在创建的excel。我想我检查了所有可能的答案,并且在过去3天里一直在苦苦挣扎。提前感谢您提供的任何帮助、建议或编辑


Tags: infogetresponserequestajaxfunctionexportlist

热门问题