首页 > 解决方案 > 如何在另一个模板语言中使用 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' 部分没有按我的预期工作。我该怎么做呢?

标签: django

解决方案


创建一个变量为

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

用这个替换你的 img 部分

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

这应该工作


推荐阅读