Django restframework 實現文件下載(json)

class DoView(APIView):
 
    def get(self,request):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        # 構造文件名 
        file_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ".json"
 
        response = Response(serializer.data)
        response["content_type"] = 'application/json'
        response['Content-Disposition'] = "attachment; filename= {}".format(file_name)



# content_type='application/octet-stream',指明下載類型

————————————————
版權聲明:本文爲CSDN博主「正牌小白牙」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_41678663/article/details/85015365

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章