Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 795
Pierre de LESPINAY Avatar asked Dec 14 '13 08:12

Pierre de LESPINAY


People also ask

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {{ name }} mean in Django templates?

8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What does {% include %} do?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

How do I use Python code in Django template?

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code. Save this answer.


1 Answers

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.

like image 55
Arpit Avatar answered Oct 18 '22 21:10

Arpit