﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21179	How-to output CSV from Django should suggest using `StreamingHttpResponse`	Simon Charette	Rigel Di Scala	"The [https://docs.djangoproject.com/en/dev/howto/outputting-csv/ Outputting CSV with Django] how-to doesn't even mention `StreamingHttpResponse` even if [https://docs.djangoproject.com/en/1.5/ref/request-response/#django.http.StreamingHttpResponse it’s useful for generating large CSV files.]

I suggest we replace the example with something along the following:

{{{#!python
import csv
from StringIO import StringIO

from django.http import StreamingHttpResponse


def some_view(request):
    rows = (
        ['First row', 'Foo', 'Bar', 'Baz'],
        ['Second row', 'A', 'B', 'C', '""Testing""', ""Here's a quote""]
    )

    # Define a generator to stream data directly to the client
    def stream():
        buffer_ = StringIO()
        writer = csv.writer(buffer_)
        for row in rows:
            writer.writerow(row)
            buffer_.seek(0)
            data = buffer_.read()
            buffer_.seek(0)
            buffer_.truncate()
            yield data

    # Create the streaming response  object with the appropriate CSV header.
    response = StreamingHttpResponse(stream(), content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=""somefilename.csv""'

    return response
}}}"	Cleanup/optimization	closed	Documentation	dev	Normal	fixed	afraid-to-commit		Accepted	1	0	0	0	1	0
