user2106353
user2106353

Reputation: 1249

Nested loops in template

I have a loop in my Django template which displays the data from database

{% for i in prosize %}
            <li><a  class="order" id="{{i.option1}}" href="javascript:setSize('{{i.option1}}')">{{i.option1}}</a></li>
" >{{i.option1}}</a></li>
        {% endfor %} 

I need to change the style for the first element and remains the same for the others like for the first element the background colour will be black and for others it should be any other colour.

Upvotes: 0

Views: 165

Answers (3)

Benyasca
Benyasca

Reputation: 1

you can add a custom css class for first element

{% for i in prosize %}
  <li {% if forloop.first %}class="red"{% endif %}>
      <a class="order" id="{{i.option1}}">{{i.option1}}</a>
  </li>
{% endfor %}

and css

.red {
  background: red;
}

Upvotes: 0

Ion Scerbatiuc
Ion Scerbatiuc

Reputation: 1171

You could use {% if forloop.first %} to check if this is the first iteration.

A full list of forloop constructs can be found here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

Upvotes: 1

pypat
pypat

Reputation: 1116

{{ forloop.counter }} 

should give you the count of the iteration. If it's one, you should be dealing with your first element.

Upvotes: 0

Related Questions