Botond
Botond

Reputation: 2802

Django: Accessing for loop variable in the child template

I have a base template:

base.html:

    {% for object in object_list %}  
            {%block object_attributes%} {%endblock%}
    {% endfor %}

and a child that inherits from it:

child.html

{% extends "base.html" %}

{%block object_attributes%} 
          {{block.super}}
          <td>{{ object.name }}</td>
          <td>{{ object.address }}</td>
{%endblock%}

It seems however, that the child is unable to see the variable "object". I understand it's a local variable of the for loop, but how could I still make it visible for the child?

Upvotes: 2

Views: 365

Answers (1)

Botond
Botond

Reputation: 2802

I figured it out. I have to save the loop variable first to pass it to the child:

base.html:

{% for object in object_list %}  
            {% with object as object_pass %}  
            {%block object_attributes%} {%endblock%}
            {% endwith %}
{% endfor %}

child.html

{% extends "base.html" %}

{%block object_attributes%} 
      {{block.super}}
      <td>{{ object_pass.name }}</td>
      <td>{{ object_pass.address }}</td>
{%endblock%}

Upvotes: 2

Related Questions