django 模板中通过变量替代key取字典内容

Posted on 2016-02-18 13:00  NooBkey  阅读(2976)  评论(0编辑  收藏  举报

 

模板中通过变量替代key取字典内容

 

 

templatetags/
├── get_item.py
├── __init__.py

 

 

###get_item.py

# coding=utf-8


from django import template
from django.utils.safestring import mark_safe
from django.template.base import resolve_variable, Node, TemplateSyntaxError

register = template.Library()

def key(d, key_name):
return d[key_name]
key = register.filter('key', key)

 

index.html

{% for i in list %}

  {{ dict |key i }}

{% endfor %}

 

不过通过这个取字典,效率很低

 

Copyright © 2024 NooBkey
Powered by .NET 9.0 on Kubernetes