Ticket #30310: request.diff

File request.diff, 1.1 KB (added by Mark Tranchant, 5 years ago)

New patch using getitem()

  • lib/python3.6/site-packages/django/http/request.py

    diff --git a/master/lib/python3.6/site-packages/django/http/request.py b/ticket_30310/lib/python3.6/site-packages/django/http/request.py
    index 02a127d..bcaf925 100644
    old new class HttpHeaders(CaseInsensitiveMapping):  
    378378                headers[name] = value
    379379        super().__init__(headers)
    380380
     381    def __getitem__(self, key):
     382        """
     383        Django template variables cannot contain hyphens, so to allow use
     384        of the headers object in templates, we also try to look up the
     385        variable with underscores. For example, use headers['foo-bar']
     386        in views, and {{ headers.foo_bar }} in templates.
     387        """
     388        value = None
     389        try:
     390            value = super().__getitem__(key)
     391        except KeyError:
     392            value = super().__getitem__(key.replace('_', '-'))
     393        return value
     394
    381395    @classmethod
    382396    def parse_header_name(cls, header):
    383397        if header.startswith(cls.HTTP_PREFIX):
Back to Top