python/Django
Django csv streaminghttpresponse
Redking
2022. 8. 31. 01:20
csv streaminghttpresponse는 Django에서 브라우저로 응답을 스트리밍하는 데 사용됩니다. 응답을 생성하는 데 너무 오래 걸리거나 메모리를 너무 많이 사용하는 경우 이 작업을 수행할 수 있습니다. csv 파일의 용량이 클 경우 유용하게 사용되며 전체 내용이 메모리에 내장되어 단일 조각으로 브라우저로 전송됩니다.
def get_rows():
yield 'Hello,'
yield 'there!'
def my_view(request):
return StreamingHttpResponse(
get_rows,
content_type='text/csv',
headers={'Content-Disposition': 'attachment; filename=files.csv'
)