mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #32002 -- Added headers parameter to HttpResponse and subclasses.
This commit is contained in:
committed by
Mariusz Felisiak
parent
2e7cc95499
commit
dcb69043d0
@@ -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.
|
||||
|
||||
@@ -724,6 +724,10 @@ by ``HttpResponse``.
|
||||
When using this interface, unlike a dictionary, ``del`` doesn't raise
|
||||
``KeyError`` if the header field doesn't exist.
|
||||
|
||||
You can also set headers on instantiation::
|
||||
|
||||
>>> response = HttpResponse(headers={'Age': 120})
|
||||
|
||||
For setting the ``Cache-Control`` and ``Vary`` header fields, it is recommended
|
||||
to use the :func:`~django.utils.cache.patch_cache_control` and
|
||||
:func:`~django.utils.cache.patch_vary_headers` methods from
|
||||
@@ -738,15 +742,19 @@ containing a newline character (CR or LF) will raise ``BadHeaderError``
|
||||
|
||||
The :attr:`HttpResponse.headers` interface was added.
|
||||
|
||||
The ability to set headers on instantiation was added.
|
||||
|
||||
Telling the browser to treat the response as a file attachment
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
To tell the browser to treat the response as a file attachment, use the
|
||||
``content_type`` argument and set the ``Content-Disposition`` header. For example,
|
||||
this is how you might return a Microsoft Excel spreadsheet::
|
||||
To tell the browser to treat the response as a file attachment, set the
|
||||
``Content-Type`` and ``Content-Disposition`` headers. For example, this is how
|
||||
you might return a Microsoft Excel spreadsheet::
|
||||
|
||||
>>> response = HttpResponse(my_data, content_type='application/vnd.ms-excel')
|
||||
>>> response.headers['Content-Disposition'] = 'attachment; filename="foo.xls"'
|
||||
>>> response = HttpResponse(my_data, headers={
|
||||
... 'Content-Type': 'application/vnd.ms-excel',
|
||||
... 'Content-Disposition': 'attachment; filename="foo.xls"',
|
||||
... })
|
||||
|
||||
There's nothing Django-specific about the ``Content-Disposition`` header, but
|
||||
it's easy to forget the syntax, so we've included it here.
|
||||
@@ -802,10 +810,10 @@ Attributes
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: HttpResponse.__init__(content=b'', content_type=None, status=200, reason=None, charset=None)
|
||||
.. method:: HttpResponse.__init__(content=b'', content_type=None, status=200, reason=None, charset=None, headers=None)
|
||||
|
||||
Instantiates an ``HttpResponse`` object with the given page content and
|
||||
content type.
|
||||
Instantiates an ``HttpResponse`` object with the given page content,
|
||||
content type, and headers.
|
||||
|
||||
``content`` is most commonly an iterator, bytestring, :class:`memoryview`,
|
||||
or string. Other types will be converted to a bytestring by encoding their
|
||||
@@ -829,6 +837,12 @@ Methods
|
||||
given it will be extracted from ``content_type``, and if that
|
||||
is unsuccessful, the :setting:`DEFAULT_CHARSET` setting will be used.
|
||||
|
||||
``headers`` is a :class:`dict` of HTTP headers for the response.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
The ``headers`` parameter was added.
|
||||
|
||||
.. method:: HttpResponse.__setitem__(header, value)
|
||||
|
||||
Sets the given header name to the given value. Both ``header`` and
|
||||
|
||||
@@ -57,7 +57,7 @@ Attributes
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None, charset=None, using=None)
|
||||
.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None, charset=None, using=None, headers=None)
|
||||
|
||||
Instantiates a :class:`~django.template.response.SimpleTemplateResponse`
|
||||
object with the given template, context, content type, HTTP status, and
|
||||
@@ -90,6 +90,13 @@ Methods
|
||||
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
||||
loading the template.
|
||||
|
||||
``headers``
|
||||
A :class:`dict` of HTTP headers to add to the response.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
The ``headers`` parameter was added.
|
||||
|
||||
.. method:: SimpleTemplateResponse.resolve_context(context)
|
||||
|
||||
Preprocesses context data that will be used for rendering a template.
|
||||
@@ -149,7 +156,7 @@ Methods
|
||||
Methods
|
||||
-------
|
||||
|
||||
.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, charset=None, using=None)
|
||||
.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, charset=None, using=None, headers=None)
|
||||
|
||||
Instantiates a :class:`~django.template.response.TemplateResponse` object
|
||||
with the given request, template, context, content type, HTTP status, and
|
||||
@@ -185,6 +192,13 @@ Methods
|
||||
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
||||
loading the template.
|
||||
|
||||
``headers``
|
||||
A :class:`dict` of HTTP headers to add to the response.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
The ``headers`` parameter was added.
|
||||
|
||||
The rendering process
|
||||
=====================
|
||||
|
||||
|
||||
@@ -332,6 +332,11 @@ Requests and Responses
|
||||
Both interfaces will continue to be supported. See
|
||||
:ref:`setting-header-fields` for details.
|
||||
|
||||
* The new ``headers`` parameter of :class:`~django.http.HttpResponse`,
|
||||
:class:`~django.template.response.SimpleTemplateResponse`, and
|
||||
:class:`~django.template.response.TemplateResponse` allows setting response
|
||||
:attr:`~django.http.HttpResponse.headers` on instantiation.
|
||||
|
||||
Security
|
||||
~~~~~~~~
|
||||
|
||||
|
||||
@@ -117,9 +117,10 @@ And the view::
|
||||
|
||||
def head(self, *args, **kwargs):
|
||||
last_book = self.get_queryset().latest('publication_date')
|
||||
response = HttpResponse()
|
||||
# RFC 1123 date format
|
||||
response.headers['Last-Modified'] = last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
||||
response = HttpResponse(
|
||||
# RFC 1123 date format.
|
||||
headers={'Last-Modified': last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT')},
|
||||
)
|
||||
return response
|
||||
|
||||
If the view is accessed from a ``GET`` request, an object list is returned in
|
||||
|
||||
Reference in New Issue
Block a user