{{ a.'Restaurant Name'}} or {{ a.Restaurant Name }}
将引发解析错误。
你可以在字典中进行for循环(但丑陋/效率低下):
{% for k, v in your_dict_passed_into_context %}
{% ifequal k "Restaurant Name" %}
{{ v }}
{% endifequal %}
{% endfor %}
from django import template
register = template.Library()
@register.simple_tag
def dictKeyLookup(the_dict, key):
# Try to fetch from the dict, and if it's not found return an empty string.
return the_dict.get(key, '')
并在模板中使用它,如下所示:
{% dictKeyLookup your_dict_passed_into_context "Restaurant Name" %}
或者,也许尝试重组你的字典,使其具有“更易于使用”的键。