模板中通过变量替代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 %}
不过通过这个取字典,效率很低