diff options
| author | Tom Carrick <tom@carrick.eu> | 2020-09-15 12:43:37 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-07 09:19:57 +0200 |
| commit | dcb69043d0de45bb55998fc418d93c28bc7689ae (patch) | |
| tree | ac58b959d8b749965841721e6cc2a58784bb51c3 /docs/ref/request-response.txt | |
| parent | 2e7cc95499f758a1c4aa036cbf1dcddf82a89ea2 (diff) | |
Fixed #32002 -- Added headers parameter to HttpResponse and subclasses.
Diffstat (limited to 'docs/ref/request-response.txt')
| -rw-r--r-- | docs/ref/request-response.txt | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index 8dae4455a2..17aebf2317 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -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 |
