Code

Opened 7 years ago

Closed 5 years ago

Last modified 4 years ago

#3371 closed (wontfix)

Filter to get dict's value by key name

Reported by: Alex Dedul Owned by: nobody
Component: Template system Version: master
Severity: Keywords: template filter dict key
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Filter's description is in summary. Example usage:

{{ dict|key:key_name }}

Filter:

def key(d, key_name):
    return d[key_name]
key = register.filter('key', key)

Looking that #2741 is closed as wontfix i have a little hope that it could be added to trunk, but i'll try :).

Attachments (0)

Change History (9)

comment:1 Changed 7 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

What does {{ dict|key:key_name }} add that {{ dict.key_name }} doesn't already do (the dot syntax already does a dictionary key lookup as the first attempt to resolve to a value)?

Also, the proposed implementation doesn't meet the standards for built-in filters: it doesn't catch KeyError and so could propagate an exception all the way back to the template (filters should fail silently -- in this case the appropriate behavior is to catch KeyError and return an empty string or the value of TEMPLATE_STRING_IF_INVALID).

comment:2 Changed 7 years ago by Alex Dedul

With {{ dict|key:key_name }} key_name holds actual key dict should be looked up while {{ dict.key_name }} look ups only 'key_name' key in dict. So with the former if key_name variable contains value 'test_key' {{ dict|key:key_name }} will result in

dict['test_key']

Here is improved version of filter(silly me), i will make patch against trunk, add documentation and tests in case it will be approved by core devs.

def key(d, key_name):
    try:
        value = d[key_name]
    except:
        from django.conf import settings

        value = settings.TEMPLATE_STRING_IF_INVALID

    return value
key = register.filter('key', key)

comment:3 Changed 7 years ago by Alex Dedul

Ops, another version - catch only KeyError exception:

def key(d, key_name):
    try:
        value = d[key_name]
    except KeyError:
        from django.conf import settings

        value = settings.TEMPLATE_STRING_IF_INVALID

    return value
key = register.filter('key', key)

comment:4 Changed 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Design decision needed

comment:5 Changed 6 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

This is out of scope for the built-in template engine (though it sounds like a great utility to have in your toolbox). Maybe post this to djangosnippets.org?

comment:6 Changed 5 years ago by nullie

  • Resolution wontfix deleted
  • Status changed from closed to reopened

Why is it out of scope? Implementation can be really simple:

{{
def get(d, key):

return d.get(key, )

register.filter(get)
}}

And I here questions about this again and again.

comment:7 Changed 5 years ago by nullie

def get(d, key):
    return d.get(key, '')

register.filter(get)

comment:8 Changed 5 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from reopened to closed

Reopening a ticket which has been closed by one of Django's lead developers is impolite. Try the developers' mailing list if you have followup questions.

comment:9 Changed 4 years ago by slacy

Instead of adding a special filter for this case, I think it would make sense to amend the template variable processing in general to have an extra step:

  • Dictionary lookup. Example: foobar?
  • Attribute lookup. Example: foo.bar
  • Method call. Example: foo.bar()
  • List-index lookup. Example: foo[bar]
  • Dictionary/List lookup by reference. Example: foo[resolve_variable("bar")]

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.