Use context variable to compare with forloop.revcounter in Django

53 Views Asked by At

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?

1

There are 1 best solutions below

1
Pulkit Arora On

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.