cssdjangotemplatetag

How do I use the django template tag in the css tag?


I tried to use the forloop.last template tag

<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>

How do I refactor the above source like the source below?

In the refactored source, "margin-bottom: {{margin-bottom}} px;" Error in "{{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>

Solution

  • you can try it:

    <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>