Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template compare two variables not working

I am building a pagination with Django and Bootstrap. There is this variable pageNum which is passed from my views.py and indicates the current page. I use a for loop to generate the pagination. The pagination itself is fine but when I compare forloop.counter to pageNum to add the class="active" to the current li its not working. But when I actually print the values of pageNum and forloop.counter they are identical. Here is the code:

 <ul class="pagination">
 {% for pagination in paginationInfo %}
      <li {% if forloop.counter == pageNum %} class="active" {%endif%} ><a href="{{pagination}}">{{forloop.counter}}</a></li>
 {% endfor %}
 </ul>
like image 518
Hemen Avatar asked Mar 12 '23 12:03

Hemen


1 Answers

You are probably passing the pageNum as string into the template. To compare it, you need to cast it into an int in your view or use the add filter:

<ul class="pagination">
 {% for pagination in paginationInfo %}
    <li {% if forloop.counter == pageNum|add:"0" %} class="active" {%endif%} ><a href="{{pagination}}">{{forloop.counter}}</a></li>
 {% endfor %}
</ul>
like image 53
RodrigoDela Avatar answered May 03 '23 12:05

RodrigoDela