0

I can not use context variable to compare with forloop.counter or forloop.revcounter. No error returning. But, every time if statement become false.

here is my view func-

def view_single_server(request, pk):
server = PhysicalServer.objects.get(pk=pk)
context = {
    'server' : server,
    'n':range(42)
}
return render(request,'server/view_single_server.html', context )

And Here is my templete-

 {% for i in n %}
    {% if forloop.revcounter == server.loc_in_rack %}
       <li>{{server.loc_in_rack}}</li>
    {% else %}
       <li>No Server</li>      
    {% endif %}
 {% endfor %}

What wrong am I doing?

Samrat
  • 83
  • 1
  • 5

1 Answers1

0

You write wrong condition. In this code you are not using range condition correctly. Everytime range starts from 0 to 41 and you can't compare your range with variable i. That's why it everytime return False.

  • forloop iteration is ok. forloop.counter is also returning value. if i compare as forloop.revcounter == '11' or number between 0-41, if statement returns true when it is true. – Samrat Aug 18 '21 at 10:35