3

I'm new to Django and I'm struggling with this : in my template, I want to iterate on an array defined in my view, add '.png' to the end of each value so I can use them as src value for an <img> tag that I create while iterating. Here's my code :

<table>
    <tr>
      {% for iter in array %}
          {% with 'path/to/images/'|add:iter|add:'.png' as myImg %}
              <td><img src="{% static myImg %}" alt=""></td>
          {% endwith %}
      {% endfor %}
    </tr>
  </table>

When I print myImg, its value is only '.png', without the iter value.

Maybe I can't use with tag inside a loop ? If so, how can I can concatenate my path, filename and extension ?

Thank's in advance

1 Answer 1

7

You're overcomplicating things.

All the static tag does is concatenate the value of settings.STATIC_URL with the filename. You can more easily do that yourself:

  {% get_static_prefix as STATIC_URL %}

  {% for iter in array %}
        <td><img src="{{ STATIC_URL }}{{ iter }}.png" alt=""></td>
  {% endfor %}
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed this is much more simple, I don't know much about how work tags yet, I need to read more docs. Thank's a lot !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.