django - 如何在另一个模板语言中使用 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' 部分没有按我的预期工作。我该怎么做呢?
解决方案
创建一个变量为
# Add this at the top of your page after the extends part
{% static "images" as baseUrl %}
用这个替换你的 img 部分
<img src="{{baseUrl}}/{{records}}.png" alt="">
这应该工作
推荐阅读
- regex - Scala正则表达式在第一个连字符和整个字符串之前获取字符串
- python - ValueError:使用序列设置数组元素?
- google-plus - 如何从 Google+ 个性化 URL 获取 Google 帐户 ID
- dns - 浏览器无法在持久连接上重新协商 DNS
- google-sheets - 为什么我第一次使用 SPARKLINE 函数时出现错误?
- haskell - 没有数据构造函数的存在量化
- mongodb - 通过存在一个字段对 mongodb 中的数组进行子集
- ansible - 如何为亚马逊 ec2 主机格式化一个简单的 Ansible 库存文件?
- r - 如何在R中将最后一列移到前面
- azure-application-insights - App Insights 未在 Spring Boot 应用程序中使用 RequestTelemetryFilter 进行健康检查控制器