diff options
| author | django-bot <ops@djangoproject.com> | 2023-02-28 20:53:28 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-03-01 13:03:56 +0100 |
| commit | 14459f80ee3a9e005989db37c26fd13bb6d2fab2 (patch) | |
| tree | eb62429ed696ed3a5389f3a676aecfc6d15a99cc /docs/ref/request-response.txt | |
| parent | 6015bab80e28aef2669f6fac53423aa65f70cb08 (diff) | |
Fixed #34140 -- Reformatted code blocks in docs with blacken-docs.
Diffstat (limited to 'docs/ref/request-response.txt')
| -rw-r--r-- | docs/ref/request-response.txt | 109 |
1 files changed, 55 insertions, 54 deletions
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index b1a177cca9..9b0ed76e4d 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -73,9 +73,9 @@ All attributes should be considered read-only, unless stated otherwise. A string representing the HTTP method used in the request. This is guaranteed to be uppercase. For example:: - if request.method == 'GET': + if request.method == "GET": do_something() - elif request.method == 'POST': + elif request.method == "POST": do_something_else() .. attribute:: HttpRequest.encoding @@ -186,19 +186,19 @@ All attributes should be considered read-only, unless stated otherwise. >>> request.headers {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6', ...} - >>> 'User-Agent' in request.headers + >>> "User-Agent" in request.headers True - >>> 'user-agent' in request.headers + >>> "user-agent" in request.headers True - >>> request.headers['User-Agent'] + >>> request.headers["User-Agent"] Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) - >>> request.headers['user-agent'] + >>> request.headers["user-agent"] Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) - >>> request.headers.get('User-Agent') + >>> request.headers.get("User-Agent") Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) - >>> request.headers.get('user-agent') + >>> request.headers.get("user-agent") Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) For use in, for example, Django templates, headers can also be looked up @@ -277,9 +277,9 @@ middleware class is listed in :setting:`MIDDLEWARE`. :attr:`~django.contrib.auth.models.User.is_authenticated`, like so:: if request.user.is_authenticated: - ... # Do something for logged-in users. + ... # Do something for logged-in users. else: - ... # Do something for anonymous users. + ... # Do something for anonymous users. Methods ------- @@ -304,9 +304,9 @@ Methods class MultipleProxyMiddleware: FORWARDED_FOR_FIELDS = [ - 'HTTP_X_FORWARDED_FOR', - 'HTTP_X_FORWARDED_HOST', - 'HTTP_X_FORWARDED_SERVER', + "HTTP_X_FORWARDED_FOR", + "HTTP_X_FORWARDED_HOST", + "HTTP_X_FORWARDED_SERVER", ] def __init__(self, get_response): @@ -319,8 +319,8 @@ Methods """ for field in self.FORWARDED_FOR_FIELDS: if field in request.META: - if ',' in request.META[field]: - parts = request.META[field].split(',') + if "," in request.META[field]: + parts = request.META[field].split(",") request.META[field] = parts[-1].strip() return self.get_response(request) @@ -389,22 +389,19 @@ Methods .. code-block:: pycon - >>> request.get_signed_cookie('name') + >>> request.get_signed_cookie("name") 'Tony' - >>> request.get_signed_cookie('name', salt='name-salt') + >>> request.get_signed_cookie("name", salt="name-salt") 'Tony' # assuming cookie was set using the same salt - >>> request.get_signed_cookie('nonexistent-cookie') - ... + >>> request.get_signed_cookie("nonexistent-cookie") KeyError: 'nonexistent-cookie' - >>> request.get_signed_cookie('nonexistent-cookie', False) + >>> request.get_signed_cookie("nonexistent-cookie", False) False - >>> request.get_signed_cookie('cookie-that-was-tampered-with') - ... + >>> request.get_signed_cookie("cookie-that-was-tampered-with") BadSignature: ... - >>> request.get_signed_cookie('name', max_age=60) - ... + >>> request.get_signed_cookie("name", max_age=60) SignatureExpired: Signature age 1677.3839159 > 60 seconds - >>> request.get_signed_cookie('name', False, max_age=60) + >>> request.get_signed_cookie("name", False, max_age=60) False See :doc:`cryptographic signing </topics/signing>` for more information. @@ -421,7 +418,7 @@ Methods .. code-block:: pycon - >>> request.accepts('text/html') + >>> request.accepts("text/html") True Most browsers send ``Accept: */*`` by default, so this would return @@ -453,6 +450,7 @@ Methods :class:`~xml.etree.ElementTree.ElementTree`:: import xml.etree.ElementTree as ET + for element in ET.iterparse(request): process(element) @@ -504,7 +502,7 @@ a subclass of dictionary. Exceptions are outlined here: .. code-block:: pycon - >>> QueryDict.fromkeys(['a', 'a', 'b'], value='val') + >>> QueryDict.fromkeys(["a", "a", "b"], value="val") <QueryDict: {'a': ['val', 'val'], 'b': ['val']}> .. method:: QueryDict.__getitem__(key) @@ -544,11 +542,11 @@ a subclass of dictionary. Exceptions are outlined here: .. code-block:: pycon - >>> q = QueryDict('a=1', mutable=True) - >>> q.update({'a': '2'}) - >>> q.getlist('a') + >>> q = QueryDict("a=1", mutable=True) + >>> q.update({"a": "2"}) + >>> q.getlist("a") ['1', '2'] - >>> q['a'] # returns the last + >>> q["a"] # returns the last '2' .. method:: QueryDict.items() @@ -559,7 +557,7 @@ a subclass of dictionary. Exceptions are outlined here: .. code-block:: pycon - >>> q = QueryDict('a=1&a=2&a=3') + >>> q = QueryDict("a=1&a=2&a=3") >>> list(q.items()) [('a', '3')] @@ -571,7 +569,7 @@ a subclass of dictionary. Exceptions are outlined here: .. code-block:: pycon - >>> q = QueryDict('a=1&a=2&a=3') + >>> q = QueryDict("a=1&a=2&a=3") >>> list(q.values()) ['3'] @@ -608,7 +606,7 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon - >>> q = QueryDict('a=1&a=2&a=3') + >>> q = QueryDict("a=1&a=2&a=3") >>> q.lists() [('a', ['1', '2', '3'])] @@ -619,8 +617,8 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon - >>> q = QueryDict('a=1&a=2&a=3', mutable=True) - >>> q.pop('a') + >>> q = QueryDict("a=1&a=2&a=3", mutable=True) + >>> q.pop("a") ['1', '2', '3'] .. method:: QueryDict.popitem() @@ -632,7 +630,7 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon - >>> q = QueryDict('a=1&a=2&a=3', mutable=True) + >>> q = QueryDict("a=1&a=2&a=3", mutable=True) >>> q.popitem() ('a', ['1', '2', '3']) @@ -644,7 +642,7 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon - >>> q = QueryDict('a=1&a=3&a=5') + >>> q = QueryDict("a=1&a=3&a=5") >>> q.dict() {'a': '5'} @@ -654,7 +652,7 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon - >>> q = QueryDict('a=2&b=3&b=5') + >>> q = QueryDict("a=2&b=3&b=5") >>> q.urlencode() 'a=2&b=3&b=5' @@ -664,8 +662,8 @@ In addition, ``QueryDict`` has the following methods: .. code-block:: pycon >>> q = QueryDict(mutable=True) - >>> q['next'] = '/a&b/' - >>> q.urlencode(safe='/') + >>> q["next"] = "/a&b/" + >>> q.urlencode(safe="/") 'next=/a%26b/' ``HttpResponse`` objects @@ -694,8 +692,8 @@ or :class:`memoryview`, to the :class:`HttpResponse` constructor: >>> from django.http import HttpResponse >>> response = HttpResponse("Here's the text of the web page.") >>> response = HttpResponse("Text only, please.", content_type="text/plain") - >>> response = HttpResponse(b'Bytestrings are also accepted.') - >>> response = HttpResponse(memoryview(b'Memoryview as well.')) + >>> response = HttpResponse(b"Bytestrings are also accepted.") + >>> response = HttpResponse(memoryview(b"Memoryview as well.")) But if you want to add content incrementally, you can use ``response`` as a file-like object: @@ -728,16 +726,16 @@ To set or remove a header field in your response, use .. code-block:: pycon >>> response = HttpResponse() - >>> response.headers['Age'] = 120 - >>> del response.headers['Age'] + >>> response.headers["Age"] = 120 + >>> del response.headers["Age"] You can also manipulate headers by treating your response like a dictionary: .. code-block:: pycon >>> response = HttpResponse() - >>> response['Age'] = 120 - >>> del response['Age'] + >>> response["Age"] = 120 + >>> del response["Age"] This proxies to ``HttpResponse.headers``, and is the original interface offered by ``HttpResponse``. @@ -749,7 +747,7 @@ You can also set headers on instantiation: .. code-block:: pycon - >>> response = HttpResponse(headers={'Age': 120}) + >>> 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 @@ -770,10 +768,13 @@ you might return a Microsoft Excel spreadsheet: .. code-block:: pycon - >>> response = HttpResponse(my_data, headers={ - ... 'Content-Type': 'application/vnd.ms-excel', - ... '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. @@ -1111,7 +1112,7 @@ Typical usage could look like: .. code-block:: pycon >>> from django.http import JsonResponse - >>> response = JsonResponse({'foo': 'bar'}) + >>> response = JsonResponse({"foo": "bar"}) >>> response.content b'{"foo": "bar"}' @@ -1297,7 +1298,7 @@ a file open in binary mode like so: .. code-block:: pycon >>> from django.http import FileResponse - >>> response = FileResponse(open('myfile.png', 'rb')) + >>> response = FileResponse(open("myfile.png", "rb")) The file will be closed automatically, so don't open it with a context manager. |
