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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With