gpt4 book ai didi

Django 模板 : how to show dict whose key has a dot in it? d ['key.name' ]

转载 作者:行者123 更新时间:2023-12-02 09:34:00 25 4
gpt4 key购买 nike

我有一本这样的字典:

dict_name['keyname.with.manydots']

问题:我做不到

{{ dict_name.keyname.with.manydots }} 

我知道用 Django 模板不可能做到这一点..但是你发现的最好的解决方法是什么?谢谢!

最佳答案

你可以写一个custom template filter :

from django import template

register = template.Library()

@register.filter
def get_key(value, arg):
return value.get(arg, None)

在你的模板中

{{ my_dict|get_key:"dotted.key.value" }}

关于Django 模板 : how to show dict whose key has a dot in it? d ['key.name' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com