0

This prints, when the key k exists in key2, a empty link, meaning its href is empty. The real value of k in key2 isn't empty.

# data is a dictionary
{% for k, v in data.key1.items %}
  {% if k in data.key2 %}
    <h3><a href='{{data.key2.k}}'>{{k}}</a></h3> <!-- this generates <a href>fdsfdsd</a> -->
  {% else %}
    <h3>{{ k }}</h3>
  {% endif %}
{% endfor %}

How do I fix it?

1 Answers1

2

{{data.key2.k}} is accessing the key 'k' in data.key2 which is probably not what you intended. There is now way to do dictionary lookups by a variable in django templates.

One solution would be a custom template filter like suggested here: Django template how to look up a dictionary value with a variable

Community
  • 1
  • 1
cbergmiller
  • 278
  • 1
  • 10