0

I have a list with IP Addresses. I also have a nested dictionary which uses those addresses as keys. For example:

my_list = ['1.2.3.4', '8.8.8.8']

my_dic = {'data': {'1.2.3.4': 'My First Data', '8.8.8.8': 'My Second Data'}}

In my template I am trying to this:

for each in my_list:
    print(my_dic['data'][each])

Everything works well until I hit that each key. So if I just print my_dic or my_dic[data] it will all work correctly. But when I add the each index nothing shows up in the webpage

I have printed each independently so I know that it works correctly. This also works as expected when I run the loop in my IDE so I know the logic is right.

In django my code looks like this:

{% if my_dic.data %}
    {% for each in my_list %}
      <p>{{ my_dic.data.each }}</p>
    {% endfor %}
{% endif %}

I'm new to Django so not sure if I'm overlooking something silly. Anyone know what I am missing here?

EDIT Found a duplicate HERE which covers my issue. Same issue Vishnu Ks pointed out.

Joe
  • 2,641
  • 5
  • 22
  • 43
  • First of all, your `my_dic` dict has some syntax error. Is that a typo? – JPG Jun 17 '19 at 17:16
  • Trying to iterate over a dictionary - let alone a nested one that you're accessing with variable keys - within a template is a bit of a pain with Django. This isn't an answer to your question, but I would strongly recommend considering performing this sort of logic within your view and then passing the processed list to your template for simpler and faster processing. – souldeux Jun 17 '19 at 17:16
  • apart from that, you can't create/have a dict with same key (`data`) – JPG Jun 17 '19 at 17:17
  • It was a type in the dic, I fixed it – Joe Jun 17 '19 at 17:19
  • @JPJ - the data lives in dictionary under data. There is only one data key there. – Joe Jun 17 '19 at 17:20
  • In the sample code your provided shouldn't it be `my_dic['data'][each]` instead of my_dic.data.each ? – Vishnu Ks Jun 17 '19 at 17:20
  • @souldeux Considering it works perfectly on the view side you may be right. – Joe Jun 17 '19 at 17:21
  • @VishnuKs No in a django template it is '.' notated not brackets – Joe Jun 17 '19 at 17:21
  • @Joe Okay. In that case data.each will be equalent to data["each"] right and not data[each]? That explains why its not working. – Vishnu Ks Jun 17 '19 at 17:23
  • No 100% sure, but when I print the `each` by itself it doesn't come with quotes – Joe Jun 17 '19 at 17:25

1 Answers1

0

The issue is that when you try to access data.each Django will look for data["each"] and not data[each].Since there is no key named "each" in my_dic nothing would be printed.

If you want to access the value of data[each] inside the template you need to write a custom template filter.

from django.template.defaulttags import register

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

Then change the Django template code to this.

{% if my_dic.data %}
    {% for each in my_list %}
      <p>{{ my_dic.data|get_item:each }}</p>
    {% endfor %}
{% endif %}

See Django template how to look up a dictionary value with a variable for more details.

Vishnu Ks
  • 483
  • 1
  • 5
  • 18
  • @Vishuks I just found a similar answer to this that I think will work. Do I put this in my views.py? – Joe Jun 17 '19 at 17:47
  • Yup. You can put that in your views or create a separate file for keeping custom filters. Anything will work :) – Vishnu Ks Jun 17 '19 at 17:50