django rest framework return file

后端 未结 2 1438
粉色の甜心
粉色の甜心 2020-12-31 01:05

I have the following view in my views.py -

class FilterView(generics.ListAPIView):
    model = cdx_composites_csv

    def get(self, request, format=None):
         


        
2条回答
  •  生来不讨喜
    2020-12-31 01:32

    I don't know why I had to do this - may be something internal to Django Rest Framework that doesn't allow putting custom methods onto format?

    I simply changed it to the following -

    if fileformat == 'raw':
        zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')
        response = HttpResponse(FileWrapper(zip_file), content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'
        return response
    

    Then in my URL just hit with the new value and it works fine. I'd love to know why I can't use format though to serve a file.

提交回复
热议问题