Skip to content
Advertisement

Django: Use firstof of if-else block inside blocktrans

I have two variables var1 and var2. I want to do this,

{% blocktrans %}
    value of my var is: {% firstof var1 var2 %}
{% endblocktrans%}

It gives me error that ‘blocktrans’ doesn’t allow other block tags. Because we are not allowed to use any other tag inside blocktrans, what is the solution of this kind of problem?

Advertisement

Answer

From django 1.9 onwards, you can use firstof to assign result to context.

{% firstof var1 var2 as myvar %}

{% blocktrans %}
    value of my var is: {{ myvar }}
{% endblocktrans%}

See django-docs and issue tracker for reference.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement