0

I want to define a variable in my Django template but if I set the variable within the statement I can't call it again. Is there any way to call the variable from the other part of my template? Thank you in advance!

My attemt:

{% if a > 1 %}
   {% with "string_1" as var %}
   {% endwith %}
{% elif a < 1%}
   {% with "string_2" as var %}
   {% endwith %}
{% else %}
   {% with "string_3" as var %}
   {% endwith %}
{% endif %}

{% if var='string_1' %}  #here i want to call the variable again
   <p>This is string_1</p>
{%else%}
   <p>This is not string_1</p>
bummm26
  • 159
  • 3
  • 17
  • I'm not sure how suited this is to your context but try looking at the answers [here](https://stackoverflow.com/questions/10786214/django-template-conditional-variable-assignment) – PyPingu Jan 30 '18 at 11:44
  • 1
    This won't work, because the `{% with %}` tag only sets the value inside the block. The Django template language is designed to discourage complex logic inside the template. What is your real code actually trying to do? – Alasdair Jan 30 '18 at 11:45
  • Use dictionary to solve this issue using filter tags. – Anup Yadav Jan 30 '18 at 11:50
  • Possible duplicate of [Django template conditional variable assignment](https://stackoverflow.com/questions/10786214/django-template-conditional-variable-assignment) – PyPingu Jan 30 '18 at 11:51
  • I have complex and long statements and I do want use these more times. I mean If i define a variable within the first statement, next time enough to call the variable and check the content of it and do not necessary write the long statement again. – bummm26 Jan 30 '18 at 11:52

0 Answers0