我通过了dict
addon_id_value_mapping =
{'menu': '1', 'opening_hour': '2', 'books': '3', 'doctors': '4'}
和
addon_list = [{u'menu': [{u'price': 50, u'name': u'Momo', u'price_level': u'cheap'},
{u'price': 510, u'name': u'potato', u'price_level': u'expensive'},
{u'price': 100, u'name': u'Chowmein', u'price_level': u'expensive'}]},
{u'opening_hour': [{u'close': 17, u'open': 9, u'day': 0},
{u'close': 17, u'open': 9, u'day': 1},
{u'close': 16, u'open': 8, u'day': 2}]}]
从视图到django-template。我可以使用template-tags来显示dict的值来显示,但我无法传入url字段。我需要做什么来传递存储在url参数
中的dict中的变量值{% if addon_list %}
{% for addon_name in addon_list %}
{% for key, values in addon_name.iteritems %}
<table>
<tr>
<td>
<h2>{{ key }}</h2>{# title- menu #}
</td>
<td>
<h5><a href="{% url 'addon:update_addon' spot_id addon_id %}">
update</a></h5>
</td>
{#todo <I need value of key variable above to replace addon_id. for key== menu I need 1 value>#}
</tr>
<tr>
</tr>
{% for value in values %}
{% for k,v in value.iteritems %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
</table>
{% endif %}
我正在使用Django 1.6.5
答案 0 :(得分:0)
尝试编写自定义模板过滤器:
from django import template
register = template.Library()
@register.filter(name='get_item')
def get_item(dictionary, key):
return dictionary.get(key)
并在您的模板中:
{% load getitem %}
<a href="{% url 'addon:update_addon' spot_id addon_id_value_mapping|get_item:key %}">
有关custem标签的更多信息,您可以查看custem tag