【发布时间】:2019-04-12 07:24:35
【问题描述】:
我正在尝试将 Django 变量连接到 img src。我找到了一个solution 来执行此操作,但是当我检查浏览器时,该值仍然没有显示出来。顺便说一句,我调用的值是 dict 类型,看起来像这样 >>> djangoDict 输出:{<foreignModel: ModelValue>: <Product: Item Desc...}
我能够使用 2 个花括号 {{ djangoDict }} 显示 ModelValue,但在与地址连接时我不能这样做。
所以这里是我的代码:
{% for dd in djangoDict %}
{% with 'products/anotherFolder/'|add:dd|add:'/suffix.jpg' as myImg %}
<a class="dropdown-item" data-img="{% static myImg %}">{{ dd }}</a>
{% endwith %}
{% endfor %
这是我在检查元素时得到的:
<a class="dropdown-item" data-img="/assets/suffix.jpg">ModelValue</a>
我想展示的内容:
<a class="dropdown-item" data-img="/assets/products/anotherFolder/ModelValue/suffix.jpg">ModelValue</a>
非常感谢任何可以提供帮助的人:)
【问题讨论】:
-
使用
{% for k, v in dict.items() %}循环浏览字典 -
@RockyLi 它没有出现。
img src还是一样的。输出为<a class="dropdown-item" data-img="/assets/products/suffix.jpg">Item desc...</a>
标签: python django-templates django-views django-staticfiles