Opened 6 years ago

Closed 5 years ago

#30310 closed New feature (fixed)

New HTTPRequest.headers not usable in templates because of hyphens

Reported by: Mark Tranchant Owned by: Troon
Component: HTTP handling Version: 2.2
Severity: Normal Keywords: request headers hyphen template
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With the release of 2.2, I took the opportunity from the new HTTPRequest.headers object to clean up old code using e.g. request.META['HTTP_X_REAL_IP'] to request.headers['X-Real-IP'].

However, this new approach does not work with templates, as variable lookups cannot use hyphens.

Could the object contain a parallel set of keys in underscored variables? e.g. request.headers['foo-bar'] is also available in request.headers['foo_bar'] and can be looked up with {{ request.headers.foo_bar }} in a template?

Attachments (1)

request.diff (1.1 KB ) - added by Mark Tranchant 6 years ago.
New patch using getitem()

Download all attachments as: .zip

Change History (11)

comment:1 by Mark Tranchant, 6 years ago

Has patch: set

comment:2 by Mark Tranchant, 6 years ago

Patch added.

comment:3 by Carlton Gibson, 6 years ago

Component: Template systemHTTP handling
Triage Stage: UnreviewedAccepted
Type: BugNew feature

Hi Mark.

The default answer for the template later is to implement a filter that will let you do the lookup with a string. (StackOverflow has lots of examples.)

Maybe we could allow this by adding a key.replace('_', '-') implementing HttpHeaders.__getitem__()? (Probably worth seeing what a patch there looks like anyway.)

(This instead of storing the items twice in the underlying store.)

by Mark Tranchant, 6 years ago

Attachment: request.diff added

New patch using getitem()

in reply to:  3 comment:4 by Mark Tranchant, 6 years ago

Replying to Carlton Gibson:

Hi Mark.

The default answer for the template later is to implement a filter that will let you do the lookup with a string. (StackOverflow has lots of examples.)

Maybe we could allow this by adding a key.replace('_', '-') implementing HttpHeaders.__getitem__()? (Probably worth seeing what a patch there looks like anyway.)

(This instead of storing the items twice in the underlying store.)

Yes, I found the filter solution. Whilst that's fine for fixing up user-generated problems, this new problem comes out-of-the-box in 2.2. It feels like there should be a solution along with it.

Your suggestion is much better than mine. New patch added that allows request.headers['foo-bar'] to be looked up in templates as {{ request.headers.foo_bar }} without inelegant double storage.

comment:5 by Mariusz Felisiak, 6 years ago

Thanks Mark! Are you able to send PR via GitHub?

comment:6 by Mariusz Felisiak, 6 years ago

Needs tests: set

comment:7 by Mark Tranchant, 6 years ago

Pull request now includes refactoring with Carlton's simplification (thanks!), a simple test, and some documentation.

Whilst I understand the "use a filter instead" argument, and agree with that where the user chooses to supply hyphenated variable names in the context, I believe that as this is a new problem caused by Django itself, it should include a solution that is as neat as other variable lookups. In my case, I tried to use the seemingly-neater replacement for the clunky request.META but ran into this problem.

comment:8 by Tobias Kunze, 6 years ago

Needs tests: unset
Owner: changed from nobody to Nick Pope
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak, 6 years ago

Owner: changed from Nick Pope to Troon

comment:10 by Carlton Gibson <carlton.gibson@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In a3a4f5c:

Fixed #30310 -- Added support for looking up HttpHeaders.headers using underscores.

Note: See TracTickets for help on using tickets.
Back to Top