- 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 5 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Reusable form template with generic view by roldandvg 8 years, 11 months ago
- Pagination Django with Boostrap by guilegarcia 9 years, 1 month 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.