- Author:
 - zalun
 - Posted:
 - April 2, 2009
 - Language:
 - HTML/template
 - Version:
 - Not specified
 - Score:
 - 0 (after 2 ratings)
 
Get the value associated with the key which may be represented by a varable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | @register.filter
def get( dict, key, default = '' ):
  """
  Usage: 
  view: 
  some_dict = {'keyA':'valueA','keyB':{'subKeyA':'subValueA','subKeyB':'subKeyB'},'keyC':'valueC'}
  keys = ['keyA','keyC']
  template: 
  {{ some_dict|get:"keyA" }}
  {{ some_dict|get:"keyB"|get:"subKeyA" }}
  {% for key in keys %}{{ some_dict|get:key }}{% endfor %}
  """
  try:
    return dict.get(key,default)
  except:
    return default
 | 
More like this
- Bootstrap Accordian by Netplay4 6 years, 9 months ago
 - Bootstrap theme for django-endless-pagination? by se210 9 years, 9 months ago
 - Bootstrap theme for django-endless-pagination? by se210 9 years, 9 months ago
 - Reusable form template with generic view by roldandvg 9 years, 11 months ago
 - Pagination Django with Boostrap by guilegarcia 10 years ago
 
Comments
{{ some_dict.keyA }} isn simpler than {{ some_dict|get:"keyA" }}
??
#
Yes it's simpler. But it doesn't wrok when keyA is a variable.
#
Please login first before commenting.