How to use if/else condition on Django Templates?

DjangoTemplatesDjango Templates

Django Problem Overview


I have the following dictionary passed to a render function, with sources being a list of strings and title being a string potentially equal to one of the strings in sources:

{'title':title, 'sources':sources})

In the HTML template I'd like to accomplish something among the lines of the following:

{% for source in sources %}
  <tr>
    <td>{{ source }}</td>
    <td>
      {% if title == {{ source }} %}
        Just now!
      {% endif %}
    </td>
  </tr>
{% endfor %}

However, the following block of text results in an error:

TemplateSyntaxError at /admin/start/
Could not parse the remainder: '{{' from '{{'

...with {% if title == {{ source }} %} being highlighted in red.

Django Solutions


Solution 1 - Django

You shouldn't use the double-bracket {{ }} syntax within if or ifequal statements, you can simply access the variable there like you would in normal python:

{% if title == source %}
   ...
{% endif %}

Solution 2 - Django

Sorry for comment in an old post but if you want to use an else if statement this will help you

{% if title == source %}
    Do This
{% elif title == value %}
    Do This
{% else %}
    Do This
{% endif %}

For more info see https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#if

Solution 3 - Django

{% for source in sources %}
  <tr>
    <td>{{ source }}</td>
    <td>
      {% ifequal title source %}
        Just now!
      {% endifequal %}
    </td>
  </tr>
{% endfor %}

                or


{% for source in sources %}
      <tr>
        <td>{{ source }}</td>
        <td>
          {% if title == source %}
            Just now!
          {% endif %}
        </td>
      </tr>
    {% endfor %}

See Django Doc

Solution 4 - Django

You try this.

I have already tried it in my django template.

It will work fine. Just remove the curly braces pair {{ and }} from {{source}}.

I have also added <table> tag and that's it.

After modification your code will look something like below.

{% for source in sources %}
   <table>
      <tr>
          <td>{{ source }}</td>
          <td>
              {% if title == source %}
                Just now! 
              {% endif %}
          </td>
      </tr>
   </table>
{% endfor %}

My dictionary looks like below,

{'title':"Rishikesh", 'sources':["Hemkesh", "Malinikesh", "Rishikesh", "Sandeep", "Darshan", "Veeru", "Shwetabh"]}

and OUTPUT looked like below once my template got rendered.

Hemkesh	
Malinikesh	
Rishikesh	Just now!
Sandeep	
Darshan	
Veeru	
Shwetabh	

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandall MaView Question on Stackoverflow
Solution 1 - DjangoHerman SchaafView Answer on Stackoverflow
Solution 2 - DjangoAntuView Answer on Stackoverflow
Solution 3 - DjangoshivaView Answer on Stackoverflow
Solution 4 - DjangohygullView Answer on Stackoverflow