#3371: Filter to get dict's value by key name
----------------------------------------+-----------------------------------
Reporter: Alex Dedul | Owner: adrian
Status: new | Component: Template system
Version: SVN | Resolution:
Keywords: template filter dict key | Stage: Unreviewed
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
----------------------------------------+-----------------------------------
Comment (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)
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/3371#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---