Fixed #30310 -- Added support for looking up HttpHeaders.headers using underscores.
This commit is contained in:
parent
48235ba807
commit
a3a4f5c144
|
@ -369,6 +369,10 @@ class HttpHeaders(CaseInsensitiveMapping):
|
||||||
headers[name] = value
|
headers[name] = value
|
||||||
super().__init__(headers)
|
super().__init__(headers)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
"""Allow header lookup using underscores in place of hyphens."""
|
||||||
|
return super().__getitem__(key.replace('_', '-'))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_header_name(cls, header):
|
def parse_header_name(cls, header):
|
||||||
if header.startswith(cls.HTTP_PREFIX):
|
if header.startswith(cls.HTTP_PREFIX):
|
||||||
|
|
|
@ -199,6 +199,15 @@ All attributes should be considered read-only, unless stated otherwise.
|
||||||
>>> request.headers.get('user-agent')
|
>>> request.headers.get('user-agent')
|
||||||
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
|
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
|
||||||
|
|
||||||
|
For use in, for example, Django templates, headers can also be looked up
|
||||||
|
using underscores in place of hyphens::
|
||||||
|
|
||||||
|
{{ request.headers.user_agent }}
|
||||||
|
|
||||||
|
.. versionchanged:: 3.0
|
||||||
|
|
||||||
|
Support for look ups using underscores was added.
|
||||||
|
|
||||||
.. attribute:: HttpRequest.resolver_match
|
.. attribute:: HttpRequest.resolver_match
|
||||||
|
|
||||||
An instance of :class:`~django.urls.ResolverMatch` representing the
|
An instance of :class:`~django.urls.ResolverMatch` representing the
|
||||||
|
|
|
@ -217,6 +217,9 @@ Requests and Responses
|
||||||
* Allowed :class:`~django.http.HttpResponse` to be initialized with
|
* Allowed :class:`~django.http.HttpResponse` to be initialized with
|
||||||
:class:`memoryview` content.
|
:class:`memoryview` content.
|
||||||
|
|
||||||
|
* For use in, for example, Django templates, :attr:`.HttpRequest.headers` now
|
||||||
|
allows look ups using underscores (e.g. ``user_agent``) in place of hyphens.
|
||||||
|
|
||||||
Serialization
|
Serialization
|
||||||
~~~~~~~~~~~~~
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -896,6 +896,7 @@ class RequestHeadersTests(SimpleTestCase):
|
||||||
request = WSGIRequest(self.ENVIRON)
|
request = WSGIRequest(self.ENVIRON)
|
||||||
self.assertEqual(request.headers['User-Agent'], 'python-requests/1.2.0')
|
self.assertEqual(request.headers['User-Agent'], 'python-requests/1.2.0')
|
||||||
self.assertEqual(request.headers['user-agent'], 'python-requests/1.2.0')
|
self.assertEqual(request.headers['user-agent'], 'python-requests/1.2.0')
|
||||||
|
self.assertEqual(request.headers['user_agent'], 'python-requests/1.2.0')
|
||||||
self.assertEqual(request.headers['Content-Type'], 'text/html')
|
self.assertEqual(request.headers['Content-Type'], 'text/html')
|
||||||
self.assertEqual(request.headers['Content-Length'], '100')
|
self.assertEqual(request.headers['Content-Length'], '100')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue