Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django -- Template tag in {% if %} block

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.

like image 637
Randall Ma Avatar asked Jul 07 '12 04:07

Randall Ma


People also ask

What {% include %} does in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

Why is {% extends %} tag used?

The extends tag is used to declare a parent template. It should be the very first tag used in a child template and a child template can only extend up to one parent template. To summarize, parent templates define blocks and child templates will override the contents of those blocks.

When {% extends %} is used for inheriting a template?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.


4 Answers

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 %} 
like image 158
Herman Schaaf Avatar answered Sep 21 '22 16:09

Herman Schaaf


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

like image 33
Antu Avatar answered Sep 21 '22 16:09

Antu


{% 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

like image 41
shiva Avatar answered Sep 23 '22 16:09

shiva


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    
like image 20
hygull Avatar answered Sep 21 '22 16:09

hygull