Django如何在静态引用中连接字符串

时间:2016-03-04 16:56:34

标签: html django

如何将card.set中的值添加到src="{% static 'images/Magic/4th Edition/Armageddon.full.jpg' %}"以代替第4版?

{% for card in sets %} 
<div class="one-third column portfolio-item">
   <div class="content">
      <div class="border">
         <img class="scale-with-grid" src="{% static 'images/Magic/4th Edition/Armageddon.full.jpg' %}" />
      </div>
      <center><h5 class="noline"><a href="#">{{card.set}}</a></h5></center>
   </div>
   <div class="shadow"></div>
   </div>
{% endfor %}

2 个答案:

答案 0 :(得分:1)

试试这个:

<img class="scale-with-grid" src="{% static 'images/Magic/{{ card.set }}/Armageddon.full.jpg' %}" />

编辑:

根据您的评论,您可以尝试:

{% with 'images/Magic/'|add:card.set|add:'/Armageddon.full.jpg' as image_path %}
    <img class="scale-with-grid" src="{% static image_path %}" />
{% endwith %}

答案 1 :(得分:0)

在Gocht的帮助下,我得到了它与

一起工作
    {% with card.set|slugify as image_path %}
        {% with 'images/Magic/'|add:image_path|add:'/Armageddon.full.jpg' as image_path %}
            <img class="scale-with-grid" src="{% static image_path %}" />
        {% endwith %}
    {% endwith %}