3

I send my dictionary - content - to a template which looks like this. Inside content is multiple dictionaries. One of them is named like_dict which looks like this : {1: 18} {2: 14} {3: 15} and another is named info which holds my user id. I use this code to sort it:

{% for x in info %}
{% if x.id in like_dict %}
<p> the value is: {{ like_dict.x.id }} </p>
{% endif %}
{% endfor %} 

Everything seems to be working, except the part within the paragraph tags. When I replace x.id with the number of my ID (like this: like_dict.1 ) it prints '18' just fine. When I leave it as I have it in the example (like_dict.x.id), it finds x.id in like_dict, but only prints 'the value is:' without stating the dictionary value. When I try to do this - like_dict[1] - I get an error for using brackets. Can anyone direct me towards how I would go about printing only the value when the key matches my ID?

Jay
  • 1,289
  • 3
  • 11
  • 22

2 Answers2

2

You should use square brackets to access the value of a variable key of a dict:

<p> the value is: {{ like_dict[x.id] }} </p>
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    that's what I thought. But it gives me this syntax error: Could not parse the remainder: '[x.id]' from 'like_dict[x.id]' – Jay Feb 12 '19 at 22:37
  • I don't think you're using Jinja2 then. With the default Django template language it does not support using square brackets to access dict values. Reconfigure your Django to use Jinja2 instead. – blhsing Feb 12 '19 at 22:39
  • Thank you. Anyway to do it in Django Template language? – Jay Feb 12 '19 at 22:44
  • You're welcome. Unfortunately I don't think there is a way to do it in Django Template language, which was why I had to suggest that you switch to Jinja2 as the rendering engine for your Django instead. – blhsing Feb 12 '19 at 22:50
  • This wound up working :) Just for your own future reference! {% for r, b in like_dict.items %} {% if x.id == r %}

    {{ b }}

    {% endif %} {% endfor %}
    – Jay Feb 12 '19 at 23:45
0

What I would try in your situation is storing x.id in a variable inside of the template using the "with" keyword.

  {% for x in info %}
    {% for k, v in x.items %}
      {% if k == 'id' %}
        {% if v in like_dict %}
          {% for key, value in like_dict.items %}
            {% if key == v %}
              <p>The value is: {{ value }}</p>
            {% endif %}
          {% endfor %}
        {% endif %}
      {% endif %}
    {% endfor %}
  {% endfor %}

Warning: This is a very dirty solution.

  • That comes up blank, as well. – Jay Feb 12 '19 at 22:57
  • Okay so I changed my answer providing a very dirty solution. Perhaps a better solution would be using custom django template filters. Check this post for more information on that: https://stackoverflow.com/questions/2894365/use-variable-as-dictionary-key-in-django-template – B. Gjorovski Feb 12 '19 at 23:53
  • I fixed it by adding .items to the end of the dictionary in the for loop. Much thx for the help, tho!: {% for r, b in like_dict.items %} {% if x.id == r %}

    {{ b }}

    {% endif %} {% endfor %}
    – Jay Feb 13 '19 at 00:29
  • You are welcome. Nice to know that you got things working! – B. Gjorovski Feb 13 '19 at 01:06