Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46178

Django - Template with expression

In a template I'd like evaluate this expression

assoc = service not in backends.not_associated

And store it in a variable, here is the context:

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

With am I getting this error ?

u'with' received an invalid token: u'not'

It seems with can't evaluate a boolean operation, can it ?

Upvotes: 6

Views: 6414

Answers (1)

Arpit
Arpit

Reputation: 953

You can't evaluate expressions in django templates. To do so, you must use custom template tags. You could use assignment tag for your case.

Upvotes: 7

Related Questions