要件
自定义template tags
app\templatetags\filters.py
from django import template
register = template.Library()
@register.filter(name='lookup')
def lookup(dict, arg:str, default=""):
"""从字典,通过key取value"""
if str(arg) in dict:
return dict[str(arg)]
else:
return default
@register.filter(name='get_item') #处理等价于lookup
def get_item(dictionary, key):
return dictionary.get(key)
{% load filters %} // load: Loads a custom template tag set.
<div class="has-text-right">{{ dict_tax_rate|lookup:"tax_code" }}</div>
无参数function可以直接调用,用【.】,有参数,则需要自定义tags,如上述【lookup】
Dictionary lookup, attribute lookup and list-index lookups are implemented with a dot notation
{{ my_dict.key }}
{{ my_object.attribute }}
{{ my_list.0 }}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。