0

I pass dict

addon_id_value_mapping = 
          {'menu': '1', 'opening_hour': '2', 'books': '3', 'doctors': '4'}

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 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 %}

I'm using Django 1.6.5

Community
  • 1
  • 1
Lionel
  • 604
  • 9
  • 26

1 Answers1

0

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)

and in your template:

{% load getitem %}

<a href="{% url 'addon:update_addon' spot_id addon_id_value_mapping|get_item:key %}">

for more info about custem tag you can checkcustem tag

slim_chebbi
  • 798
  • 6
  • 9