How do I store a Django comparison result with a template tag?

I would like to create a new variable in django template that will have a comparison value obj.site.profile.default_role==obj

Unfortunately, none of these codes work:

{% with obj.site.profile.default_role==obj as default %}{% endwith %}

{% with default=obj.site.profile.default_role==obj %}{% endwith %}

      

What is the correct syntax?

+3


source to share


2 answers


Tag

with

does not support value estimation.

The only possible template-only solution I can do is to split the html part into a sub-template and use the tag {% include %}



{% if obj.site.profile.default_role==obj %}
    {% include 'subtemplate.html' with default=True %}
{% else %}
    {% include 'subtemplate.html' with default=False %}
{% endif %} 

      

+2


source


with

can only accept a "normal" context variable.

You can try assignment-tags instead by passing your parameters to it.

@register.assignment_tag
def boolean_tag(default_role, obj):
    return default_role == obj

      



and in the template

{% boolean_tag obj.site.profile.default_role obj as my_check %}

      

This solution is fine if the variable is used in one block of the template (for example, in your case when you are trying to use with

). If you need a variable in multiple page blocks, it is better to add it to the page context with the taginclude

+2


source







All Articles