summaryrefslogtreecommitdiff
path: root/docs/howto/outputting-csv.txt
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2023-02-28 20:53:28 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-01 13:03:56 +0100
commit14459f80ee3a9e005989db37c26fd13bb6d2fab2 (patch)
treeeb62429ed696ed3a5389f3a676aecfc6d15a99cc /docs/howto/outputting-csv.txt
parent6015bab80e28aef2669f6fac53423aa65f70cb08 (diff)
Fixed #34140 -- Reformatted code blocks in docs with blacken-docs.
Diffstat (limited to 'docs/howto/outputting-csv.txt')
-rw-r--r--docs/howto/outputting-csv.txt27
1 files changed, 16 insertions, 11 deletions
diff --git a/docs/howto/outputting-csv.txt b/docs/howto/outputting-csv.txt
index 6966466921..8e4bd8108c 100644
--- a/docs/howto/outputting-csv.txt
+++ b/docs/howto/outputting-csv.txt
@@ -18,16 +18,17 @@ Here's an example::
import csv
from django.http import HttpResponse
+
def 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"'},
+ 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"])
+ writer.writerow(["First row", "Foo", "Bar", "Baz"])
+ writer.writerow(["Second row", "A", "B", "C", '"Testing"', "Here's a quote"])
return response
@@ -72,14 +73,17 @@ the assembly and transmission of a large CSV file::
from django.http import StreamingHttpResponse
+
class Echo:
"""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
@@ -91,7 +95,7 @@ the assembly and transmission of a large CSV file::
return StreamingHttpResponse(
(writer.writerow(row) for row in rows),
content_type="text/csv",
- headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},
+ headers={"Content-Disposition": 'attachment; filename="somefilename.csv"'},
)
Using the template system
@@ -109,22 +113,23 @@ Here's an example, which generates the same CSV file as above::
from django.http import HttpResponse
from django.template import loader
+
def 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"'},
+ 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"),
+ ("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}
+ t = loader.get_template("my_template_name.txt")
+ c = {"data": csv_data}
response.write(t.render(c))
return response