Skip to content
Browse files

Fixed #20459 - Improved example for setting HTTP header fields.

Thanks Jérémie Blaser.
  • Loading branch information...
1 parent 6786920 commit e83ff42792eb52235cacda58f3441673cc4e4c94 @timgraham timgraham committed
Showing with 10 additions and 2 deletions.
  1. +1 −0 AUTHORS
  2. +9 −2 docs/ref/request-response.txt
@@ -111,6 +111,7 @@ answer newbie questions, and generally made Django that much better:
Paul Bissex <>
Loïc Bistuer <>
Simon Blanchard
+ Jérémie Blaser <>
Craig Blaszczyk <>
David Blewett <>
Artem Gnilov <>
11 docs/ref/request-response.txt
@@ -584,12 +584,19 @@ Setting header fields
To set or remove a header field in your response, treat it like a dictionary::
>>> response = HttpResponse()
- >>> response['Cache-Control'] = 'no-cache'
- >>> del response['Cache-Control']
+ >>> response['Age'] = 120
+ >>> del response['Age']
Note that unlike a dictionary, ``del`` doesn't raise ``KeyError`` if the header
field doesn't exist.
+For setting the ``Cache-Control`` and ``Vary`` header fields, it is recommended
+to use the :meth:`~django.utils.cache.patch_cache_control` and
+:meth:`~django.utils.cache.patch_vary_headers` methods from
+:mod:`django.utils.cache`, since these fields can have multiple, comma-separated
+values. The "patch" methods ensure that other values, e.g. added by a
+middleware, are not removed.
HTTP header fields cannot contain newlines. An attempt to set a header field
containing a newline character (CR or LF) will raise ``BadHeaderError``

0 comments on commit e83ff42

Please sign in to comment.
Something went wrong with that request. Please try again.