Last active
August 29, 2015 14:16
-
-
Save tanish-kr/af92dc81c7c0af8af54e to your computer and use it in GitHub Desktop.
djangoカスタムフィルタの引数について ref: http://qiita.com/tatsu_nishiki/items/0543704d2c86459a1063
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ dict_obj|get_dict_val:'key1,key2' }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"1":{"0001":"営業部"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# エラー | |
{% for row in data %} | |
<p>{{ dict_obj|get_dict_val:row.no,row.busho_code }}</p> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def join_comma(var,args): | |
return "%s,%s" % (var,args) | |
register.filter('join_comma',join_comma) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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