首页 > 编程语言 > 详细

【Python】Django 支持 restful 风格 url

时间:2016-10-21 16:13:31      阅读:275      评论:0      收藏:0      [点我收藏+]

URL通配符示例:

url(r^file_download/(?P<filename>(.)*)$, views.FILE_DOWNLOAD_VIEW.as_view()),

 

代码示例:

    def get(self, request, filename):
        from common.s3_storage import S3_STORAGE
        s3_storage = S3_STORAGE()
        key = s3_storage.package_bucket.get_key(filename)
        assert(key is not None)
        
        import tempfile
        temp = tempfile.NamedTemporaryFile()
        try:
            #temp = tempfile.TemporaryFile()
            key.get_contents_to_filename(temp.name)
            temp.seek(0)
            c = temp.read()
            
            #c = key.get_contents_as_string()
            #return APIResponse(status=status_code.success, data=OS_AND_KERNEL_MAPS)
            from django.http import StreamingHttpResponse
            #response = HttpResponse(c)
            response = StreamingHttpResponse(c)
            #response = StreamingHttpResponse( self.file_iterator(temp) )
            
            response[Content-Type] = application/octet-stream
            import os
            response[Content-Length] = os.path.getsize(temp.name)
            response[Content-Disposition] = attachment;filename="{0}".format(filename)
            return response
        finally:
            temp.close()

 

【Python】Django 支持 restful 风格 url

原文:http://www.cnblogs.com/junneyang/p/5984624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!