将变量值存储在字典到url参数中 [英] pass variable value store in dictionary to url parameter
本文介绍了将变量值存储在字典到url参数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过dict
addon_id_value_mapping =
{'menu': '1', 'opening_hour': '2', 'books': '3', 'doctors': '4'}
/ p>
and
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}]}]
from查看django-template。我可以使用模板标签访问dict的值显示,但我无法通过url字段。我需要做什么来传递存储在dict中的变量值url参数
from view to django-template. I can access the value of the dict using template-tags to display but I couldn't able to pass in url field. What should I need to do to pass variable value that store in dict in url parameter
{% 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
I'm using Django 1.6.5
推荐答案
尝试编写自定义模板过滤器:
try to write custom template filter:
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标签
for more info about custem tag you can checkcustem tag
这篇关于将变量值存储在字典到url参数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文