【问题标题】:How do I use the django template tag in the css tag?如何在 css 标签中使用 django 模板标签?
【发布时间】:2017-09-04 14:51:08
【问题描述】:

我尝试使用 forloop.last 模板标签

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

【问题讨论】:

  • {{margin-bottom}} 这将导致错误,因为- 被认为是减号

标签: css django templatetag


【解决方案1】:

你可以试试:

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

【讨论】:

    猜你喜欢
    • 2014-03-20
    • 2015-02-27
    • 1970-01-01
    • 2022-11-25
    • 2018-11-19
    • 2016-10-22
    • 2013-10-04
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多