django休息框架返回文件 [英] django rest framework return file
问题描述
class FilterView(generics.ListAPIView):
vendor = self.request.GET.get('vendor')
文件名= self.request(自我,请求,格式=无) 。$。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ .get('filedate')
snippets = model.objects.using('markitdb')。filter(Date__contains = filedate)
serializer = cdx_compositesSerializer(snippets,many = True)
如果格式=='raw':
zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip','rb')
response = HttpResponse(zip_file,content_type =' application / force-download')
response ['Content-Disposition'] ='attachment;文件名=%s'%'CDX_COMPOSITES_20140626.zip'
返回响应
其他:
返回响应(serializer.data)
它适用于xml,json,csv,但是当我尝试使用raw时,它不会返回文件,而是提供detail:Not发现为什么会发生这种情况?
我打的网址如下 -
工作 -
http:// dt-rpittom:8000 / testfilter /?vendor = markit& filename = cdx_composites.csv& filedate = 2014 -06-26& format = json
这应该返回一个zip文件进行下载。
http:// dt-rpittom:8000 / testfilter /?vendor = markit& filename = cdx_composites.csv& filedate = 2014-06-26& format = raw
我不知道为什么我必须这样做 - 可能是Django Rest Framework内部的东西,允许定制我方式到格式?
我只是将其更改为以下 -
如果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;文件名=%s'%'CDX_COMPOSITES_20140626.zip'
返回响应
然后在我的URL只是用新的值,它的工作正常。我很想知道为什么我不能使用格式来提供文件。
I have the following view in my views.py -
class FilterView(generics.ListAPIView):
model = cdx_composites_csv
def get(self, request, format=None):
vendor = self.request.GET.get('vendor')
filename = self.request.GET.get('filename')
tablename = filename.replace(".","_")
model = get_model(vendor, tablename)
filedate = self.request.GET.get('filedate')
snippets = model.objects.using('markitdb').filter(Date__contains=filedate)
serializer = cdx_compositesSerializer(snippets, many=True)
if format == 'raw':
zip_file = open('C:\temp\core\files\CDX_COMPOSITES_20140626.zip', 'rb')
response = HttpResponse(zip_file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % 'CDX_COMPOSITES_20140626.zip'
return response
else:
return Response(serializer.data)
It works great for xml, json, csv but when I try to use raw it's not returning the file instead it's giving ""detail": "Not found"" why is this happening?
The URL I'm hitting is as follows -
json example that works -
http://dt-rpittom:8000/testfilter/?vendor=markit&filename=cdx_composites.csv&filedate=2014-06-26&format=json
This should return a zip file for download.
http://dt-rpittom:8000/testfilter/?vendor=markit&filename=cdx_composites.csv&filedate=2014-06-26&format=raw
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.
这篇关于django休息框架返回文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!