========================How to create CSV output========================This document explains how to output CSV (Comma Separated Values) dynamicallyusing Django views. To do this, you can either use the Python CSV library or theDjango template system.Using the Python CSV library============================Python comes with a CSV library, :mod:`csv`. The key to using it with Django isthat the :mod:`csv` module's CSV-creation capability acts on file-like objects,and Django's :class:`~django.http.HttpResponse` objects are file-like objects.Here's an example::import csvfrom django.http import HttpResponsedef some_view(request):# Create the HttpResponse object with the appropriate CSV header.response = HttpResponse(content_type='text/csv',headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},)writer = csv.writer(response)writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])return responseThe code and comments should be self-explanatory, but a few things deserve amention:* The response gets a special MIME type, :mimetype:`text/csv`. This tellsbrowsers that the document is a CSV file, rather than an HTML file. Ifyou leave this off, browsers will probably interpret the output as HTML,which will result in ugly, scary gobbledygook in the browser window.* The response gets an additional ``Content-Disposition`` header, whichcontains the name of the CSV file. This filename is arbitrary; call itwhatever you want. It'll be used by browsers in the "Save as..." dialog, etc.* You can hook into the CSV-generation API by passing ``response`` as the firstargument to ``csv.writer``. The ``csv.writer`` function expects a file-likeobject, and :class:`~django.http.HttpResponse` objects fit the bill.* For each row in your CSV file, call ``writer.writerow``, passing it an:term:`iterable`.* The CSV module takes care of quoting for you, so you don't have to worryabout escaping strings with quotes or commas in them. Pass ``writerow()``your raw strings, and it'll do the right thing... _streaming-csv-files:Streaming large CSV files-------------------------When dealing with views that generate very large responses, you might want toconsider using Django's :class:`~django.http.StreamingHttpResponse` instead.For example, by streaming a file that takes a long time to generate you canavoid a load balancer dropping a connection that might have otherwise timed outwhile the server was generating the response.In this example, we make full use of Python generators to efficiently handlethe assembly and transmission of a large CSV file::import csvfrom django.http import StreamingHttpResponseclass Echo:"""An object that implements just the write method of the file-likeinterface."""def write(self, value):"""Write the value by returning it, instead of storing in a buffer."""return valuedef 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 {}".format(idx), str(idx)] for idx in range(65536))pseudo_buffer = Echo()writer = csv.writer(pseudo_buffer)return StreamingHttpResponse((writer.writerow(row) for row in rows),content_type="text/csv",headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},)Using the template system=========================Alternatively, you can use the :doc:`Django template system </topics/templates>`to generate CSV. This is lower-level than using the convenient Python :mod:`csv`module, but the solution is presented here for completeness.The idea here is to pass a list of items to your template, and have thetemplate output the commas in a :ttag:`for` loop.Here's an example, which generates the same CSV file as above::from django.http import HttpResponsefrom django.template import loaderdef some_view(request):# Create the HttpResponse object with the appropriate CSV header.response = HttpResponse(content_type='text/csv',headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},)# The data is hard-coded here, but you could load it from a database or# some other source.csv_data = (('First row', 'Foo', 'Bar', 'Baz'),('Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"),)t = loader.get_template('my_template_name.txt')c = {'data': csv_data}response.write(t.render(c))return responseThe only difference between this example and the previous example is that thisone uses template loading instead of the CSV module. The rest of the code --such as the ``content_type='text/csv'`` -- is the same.Then, create the template ``my_template_name.txt``, with this template code:.. code-block:: html+django{% for row in data %}"{{ row.0|addslashes }}", "{{ row.1|addslashes }}", "{{ row.2|addslashes }}", "{{ row.3|addslashes }}", "{{ row.4|addslashes }}"{% endfor %}This short template iterates over the given data and displays a line of CSV foreach row. It uses the :tfilter:`addslashes` template filter to ensure therearen't any problems with quotes.Other text-based formats========================Notice that there isn't very much specific to CSV here -- just the specificoutput format. You can use either of these techniques to output any text-basedformat you can dream of. You can also use a similar technique to generatearbitrary binary data; see :doc:`/howto/outputting-pdf` for an example.