Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
Tasawer Nawaz Avatar asked Mar 13 '23 18:03

Tasawer Nawaz


1 Answers

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.

like image 85
v1k45 Avatar answered Mar 24 '23 12:03

v1k45