Fixed #32002 -- Added headers parameter to HttpResponse and subclasses.

This commit is contained in:
Tom Carrick 2020-09-15 12:43:37 +02:00 committed by Mariusz Felisiak
parent 2e7cc95499
commit dcb69043d0
9 changed files with 115 additions and 32 deletions

View file

@ -20,8 +20,10 @@ Here's an example::
def some_view(request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response.headers['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
response = HttpResponse(
content_type='text/csv',
headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},
)
writer = csv.writer(response)
writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
@ -86,10 +88,11 @@ the assembly and transmission of a large CSV file::
rows = (["Row {}".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.headers['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
return response
return StreamingHttpResponse(
(writer.writerow(row) for row in rows),
content_type="text/csv",
headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},
)
Using the template system
=========================
@ -108,8 +111,10 @@ Here's an example, which generates the same CSV file as above::
def some_view(request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response.headers['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
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.