Skip to content

Instantly share code, notes, and snippets.

@tanish-kr
Last active August 29, 2015 14:16
Show Gist options
  • Save tanish-kr/af92dc81c7c0af8af54e to your computer and use it in GitHub Desktop.
Save tanish-kr/af92dc81c7c0af8af54e to your computer and use it in GitHub Desktop.
djangoカスタムフィルタの引数について ref: http://qiita.com/tatsu_nishiki/items/0543704d2c86459a1063
from django import template
register = template.Library()
def get_dict_val(var,args):
dict_val = var
keys = [arg.strip() for arg in args.split(',')]
for key in keys:
dict_val = dict_val[key]
return dict_val
register.filter('dict_val',get_dict_val)
{{ dict_obj|get_dict_val:'key1,key2' }}
{"1":{"0001":"営業部"}}
# エラー
{% for row in data %}
<p>{{ dict_obj|get_dict_val:row.no,row.busho_code }}</p>
{% endfor %}
def join_comma(var,args):
return "%s,%s" % (var,args)
register.filter('join_comma',join_comma)
{% for row in data %}
{% with row.no|join_comma:row.busho_code as shain_id %}
<p>{{ dict_obj|get_dict_val:shain_id }}</p>
{% endwith %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment