2017-09-04 179 views
1

我试图用forloop.last模板标签如何在css标签中使用django模板标签?

<div class="panel-body"> 
{% for card in cardlist.card_set.all %} 
    {% if forloop.last %} 
    <div class="well" style="margin-bottom: 0px;">{{ card.title }}</div> 
    {% else %} 
    <div class="well" style="margin-bottom: 20px;">{{ card.title }}</div> 
    {% endif %} 
{% endfor %} 
</div> 

如何重构上面的源代码像下面的来源?

在重构源中,“margin-bottom:{{margin-bottom}} px;” “{{margin-bottom}}”中出现错误。

<div class="panel-body"> 
{% for card in cardlist.card_set.all %} 
    {% if forloop.last %} 
     margin-bottom = 0 
    {% else %} 
     margin-bottom = 20 
    {% endif %} 
    <div class="well" style="margin-bottom: {{ }}px;">{{ card.title }}</div> 
{% endfor %} 
</div> 
+1

'{{边距}}'这将导致错误,因为'-'被认为是减 –

回答

0

你可以尝试:

<div class="panel-body"> 
{% for card in cardlist.card_set.all %} 
    <div class="well" style="margin-bottom:{% if forloop.last %}0px{% else %}20px{% endif %};">{{ card.title }}</div> 
{% endfor %} 
</div>