0

I can do this in regular python, but can't seem to do this in the template.

So here are my variables:

PropertyID = ['A1','A2','A3']
filePaths = {A1:'['file1.jpg','file2.jpg','file3.jpg',]', A2:'['file1.jpg','file2.jpg','file3.jpg']'}

My template:

{% for properties in PropertyID %}
  {% for filePaths in imageFiles %}
    {% for singleFiles in filePaths %}

       

    {% endfor %}
  {% endfor %}
{% endfor %}

I want to be able to iterate through filePaths dynamically, but when I try:

{% for singleFiles in filePaths.{{properties}} %}

I get an error:

Could not parse the remainder: '{{properties}}' from 'filePaths.{{properties}}'

I have tried filePaths['properties'] and filePaths[properties] and pretty much every combination. I just can't figure out why this isn't working.

If I do filePaths.A1 it works fine, but I can't iterate over the rest of the paths this way.

Any help would be much appreciated, probably something really dumb I missed.

PacketLoss
  • 5,561
  • 1
  • 9
  • 27
OneAdamTwelve
  • 236
  • 1
  • 3
  • 14

1 Answers1

0

You can't access dictionary keys like that, instead you can use {% if %} logic in the teample:

{% for properties in PropertyID %}
  {% for filePaths in imageFiles %}
    {% for propertyKey, singleFiles in filePaths.items %}
        {% if propertyKey == properties %}
           {% for singleFile in singleFiles %}
             // your code
           {% endfor %}
       {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

FYI, it is better to do these operations in views instead of templates.

ruddra
  • 50,746
  • 7
  • 78
  • 101