Lunartist
Lunartist

Reputation: 464

How can I use Django template language inside another one?

I'm trying to use a static image with a file name decided by forloop in Django template.

{% for items in dictionary %}
    <tr>
    {% for records in items %}
        {% if forloop.counter == 2 %} 
            <td><img src="{% static '{{ records }}.png' %}"></td>
        {% else %}
            <td>{{ records }}</td>
        {% endif %}
    {% endfor %}
    </tr>
{% endfor %}

It turned out '{{ records }}.png' part didn't work as I intended. How do I do this?

Upvotes: 1

Views: 65

Answers (1)

Himanshu Poddar
Himanshu Poddar

Reputation: 7799

Create a variable as

# Add this at the top of your page after the extends part
{% static "images" as baseUrl %}

replace your img part with this

<img src="{{baseUrl}}/{{records}}.png" alt="">

This should work

Upvotes: 1

Related Questions