我正在尝试将Django变量连接到img src
。我发现有solution可以这样做,但是当我检查浏览器时,仍然没有显示该值。顺便说一句,我正在调用的值是dict
类型,看起来像这样>>> djangoDict
输出:{<foreignModel: ModelValue>: <Product: Item Desc...}
我能够使用2个花括号ModelValue
来显示{{ djangoDict }}
,但在与地址连接时却无法显示。
这是我的代码:
{% 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>
非常感谢任何能帮助您的人:)
答案 0 :(得分:0)
我看到了与我类似的问题,该问题解决了连接图像路径的问题。 It is the best answer回答我的问题。
我使用了STATIC_URL
,它是在settings.py上配置的。
我的代码现在看起来像这样:
{% for dd in djangoDict %}
<a class="dropdown-item" data-img="{{ STATIC_URL }}products/anotherFolder/{{ dd }}/suffix.jpg">{{ dd }}</a>
{% endfor %}
答案 1 :(得分:0)
我还发现自己面临着类似的问题,我不知道它是否完全相同。我想将静态文件夹中的文件链接到HTML文件,即src=""
,该目录应来自字典源。
显然,您可以直接将其链接到那里。这是我的代码的一部分;
{% for dic in posts %}
{% if dic.no == 1 %}
<img alt="" class="u-expanded-width u-image u-image-default src="{% static dic.src %}"><!--/product_image--><!--product_title-->
<h4 class="u-product-control u-text u-text-body-alt-color u-text-2">
<a class="u-product-title-link" href="#"><!--product_title_content-->{{ dic.name }}<!--/product_title_content--></a>
{% endif %}
{% endfor %}