dict in template

HI, beginner question. I spent several hours trying to find out how to do this but I’m stuck.

I’m trying to iterate through this dict in a template and finally on the right place access the “text” field.

I tried something like

{% for meaning in meanings %}
{% for field in meaning %}
{% for translations in field %}
{% for lang in translations %}
{% for entry in lang %}
{{ entry.text }} ??? but this doesn’t show any content

    {
      "meanings": [
        {
          "translations": {
            "de": [
              {
                "text": "Positives Recht",
                "rod": "n"
              }
            ],
            "en": [
              {
                "text": "positive law"
              }
            ],
            "fr": [
              {
                "text": "droit positif",
                "rod": "m"
              }
            ],
            "la": [
              {
                "text": "ius positivum",
                "rod": "n"
              }
            ]
          }
        }
      ],
    }

The third example in the docs for the for tag show how to iterate over the keys and values of a dictionary.

Yeah, I read it several times. But when I try to access “translations” via

{% for meaning in meanings %}
{% if meaning == “translations” %}
{% for lang, data in meaning.items %}
{{ lang }} : {{ data }}

it doesn’t print anything

Ok, so meanings is a list.

for meaning in meanings gives you an element of that list, which is a dict.

meaning.translations should give you the value of the key named translations in that dict. This value is itself a dict.

meaning.translations.de should then give you a list.

If you want all translations, then you’re looking at something like for code, code_list in meaning.translations.items.

This should get you farther along.

Thank you, finally I understood that there is a difference between looping a list and a dict.

One more question, since now I have several nested for loops - it looks like

{% for B in A %}
{% for C, D in B.items %}
{% for E, F in D.items %}
{% for G in F %}

how can I get forloop.counter of B? something like forloop.parentloop.parentloop.parentloop.counter? :slight_smile:

That’s one way of doing it, yes.

I’d be more inclined to use the with tag in the outermost loop to set a local variable to forloop.counter, and then reference that local variable anywhere within the nested set.