【发布时间】:2018-07-13 18:22:15
【问题描述】:
所以我试图通过一个简单的 Django Rest 框架函数视图来提供一个静态文件。它给了我 200 个代码,但不下载文件。
代码如下:
@api_view(['POST'])
def download_file(request):
if request.method == 'POST':
serializer = MySerializer(data=request.data)
filename = 'file.xlsx'
file_full_path = "src/{0}".format(filename)
with open(file_full_path, 'rb') as f:
file = f.read()
response = HttpResponse(file, content_type="application/xls")
response['Content-Disposition'] = "attachment; filename={0}".format(filename)
response['Content-Length'] = os.path.getsize(file_full_path)
return response
return Response(status=status.HTTP_400_BAD_REQUEST)
我在这里做错了什么?
【问题讨论】:
-
您好,您的代码在 MySerializer 中运行良好检查:您需要这个吗?
标签: django python-3.x django-rest-framework django-views