如何在另一种语言中使用Django模板语言?

时间:2019-10-27 04:49:41

标签: django

我正在尝试使用静态图像,其文件名由Django模板中的forloop决定。

{% 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 %}

原来,“ {{records}}。png”部分没有按我的预期工作。我该怎么做?

1 个答案:

答案 0 :(得分:1)

创建一个变量

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

为此替换您的img部分

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

这应该有效

相关问题