#21179: How-to output CSV from Django should suggest using 
`StreamingHttpResponse`
------------------------------------------------+------------------------
               Reporter:  charettes             |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Documentation         |        Version:  master
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  1                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  1
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 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
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21179>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.8ed34b4c386eccd911c4ae97368f5692%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to