【问题标题】:Django - Template with expressionDjango - 带有表达式的模板
【发布时间】:2014-01-02 01:59:17
【问题描述】:

我想在模板中评估这个表达式

assoc = service not in backends.not_associated

并将其存储在一个变量中,这是上下文:

<ul class="list-inline">
{% for service in backends.backends %}
    {% with assoc=service not in backends.not_associated %}
    <li{% if assoc %} class="associated"{% endif %}>
        <a rel="nofollow"
            href="{% url
                'social:'|add:assoc|yesno:'begin,disconnect' service %}"
            title="{{ service|title }}">
            <img src="{% static 'social_icons/'|add:service|add:'.png' %}" />
        </a>
    </li>
    {% endwith %}
{% endfor %}
</ul>

我会收到这个错误吗?

u'with' 收到无效令牌:u'not'

似乎with 无法计算布尔运算,是吗?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您不能评估 django 模板中的表达式。为此,您必须使用custom template tags。您可以使用assignment tag 来处理您的情况。

    【讨论】:

    • 好的,我明白了,分配标签似乎确实是我要走的路。我错误地认为 with 是为评估表达式而制作的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    相关资源
    最近更新 更多