首页 > 代码库 > 【Python】Django 支持 restful 风格 url
【Python】Django 支持 restful 风格 url
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=http://www.mamicode.com/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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。