summaryrefslogtreecommitdiff
path: root/docs/howto/outputting-csv.txt
diff options
context:
space:
mode:
authorzedr <rigel.discala@propylon.com>2014-03-04 22:40:44 +0000
committerTim Graham <timograham@gmail.com>2014-03-17 07:22:27 -0400
commitfad47367bf622635b4cf931db72310cce41cebb4 (patch)
tree04489e758801e3c63a366338f0b16d28fe4faf9e /docs/howto/outputting-csv.txt
parent584044566414d3f4595337225aa23b52a089a680 (diff)
Fixed #21179 -- Added a StreamingHttpResponse example for CSV files.
Thanks charettes for the suggestion.
Diffstat (limited to 'docs/howto/outputting-csv.txt')
-rw-r--r--docs/howto/outputting-csv.txt40
1 files changed, 40 insertions, 0 deletions
diff --git a/docs/howto/outputting-csv.txt b/docs/howto/outputting-csv.txt
index 1f9efb5a4b..8e7c311757 100644
--- a/docs/howto/outputting-csv.txt
+++ b/docs/howto/outputting-csv.txt
@@ -75,6 +75,46 @@ For more information, see the Python documentation of the :mod:`csv` module.
.. _`csv module's examples section`: http://docs.python.org/library/csv.html#examples
.. _`python-unicodecsv module`: https://github.com/jdunck/python-unicodecsv
+.. _streaming-csv-files:
+
+Streaming large CSV files
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+When dealing with views that generate very large responses, you might want to
+consider using Django's :class:`~django.http.StreamingHttpResponse` instead.
+For example, by streaming a file that takes a long time to generate you can
+avoid a load balancer dropping a connection that might have otherwise timed out
+while the server was generating the response.
+
+In this example, we make full use of Python generators to efficiently handle
+the assembly and transmission of a large CSV file::
+
+ import csv
+
+ from django.utils.six.moves import range
+ from django.http import StreamingHttpResponse
+
+ class Echo(object):
+ """An object that implements just the write method of the file-like
+ interface.
+ """
+ def write(self, value):
+ """Write the value by returning it, instead of storing in a buffer."""
+ return value
+
+ def some_streaming_csv_view(request):
+ """A view that streams a large CSV file."""
+ # Generate a sequence of rows. The range is based on the maximum number of
+ # rows that can be handled by a single sheet in most spreadsheet
+ # applications.
+ rows = (["Row {0}".format(idx), str(idx)] for idx in range(65536))
+ pseudo_buffer = Echo()
+ writer = csv.writer(pseudo_buffer)
+ response = StreamingHttpResponse((writer.writerow(row) for row in rows),
+ content_type="text/csv")
+ response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
+ return response
+
Using the template system
=========================