Permalink
Browse files

better example for set http header field, mention of header patch met…

…hods
  • Loading branch information...
1 parent 9bab4ea commit 227f4bf4fe7971754ecc09aeb141b824a3f0550b bbjay committed May 19, 2013
Showing with 9 additions and 2 deletions.
  1. +9 −2 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 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 mulitple, comma-separated
+values. The patch-methods ensure that other present 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 227f4bf

Please sign in to comment.