kitaro-tn
3/5/2015 - 3:03 AM

djangoカスタムフィルタの引数について ref: http://qiita.com/tatsu_nishiki/items/0543704d2c86459a1063

djangoカスタムフィルタの引数について ref: http://qiita.com/tatsu_nishiki/items/0543704d2c86459a1063

{% 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 %}
def join_comma(var,args):
    return "%s,%s" % (var,args)
register.filter('join_comma',join_comma)
# エラー
{% for row in data %}
  <p>{{ dict_obj|get_dict_val:row.no,row.busho_code }}</p>
{% endfor %}
{"1":{"0001":"営業部"}}
{{ dict_obj|get_dict_val:'key1,key2' }}
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)