django paginator页码不能用于if

时间:2016-05-11 21:26:54

标签: django pagination

我有以下基于模板的分页:

{% for ipage in transactions.paginator.page_range %}

    <li {% if ipage == page %} class="active"{%endif%}><a href="?page={{ ipage }}">{{ipage}} - {{page}}</a></li>
{% endfor %}

视图页面如下所示:

trans_list = Transaction.objects.all()

    paginator = Paginator(trans_list, 15) 

    page = request.GET.get('page')
    try:
        transactions = paginator.page(page)
    except PageNotAnInteger:
        transactions = paginator.page(1)
    except EmptyPage:
        transactions = paginator.page(paginator.num_pages)

    context = {
        'page':page,
        'transactions':transactions,
    }

ipagepage都打印了页码,但if在for循环中匹配时不显示活动类。

当页码和for循环索引匹配时,如何获得if匹配?

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

{% for ipage in transactions.paginator.page_range %}
    {% ifequal ipage transactions.number %}
        <!-- Do something special for this page -->
    {% else %}
        <!-- All the other pages -->
    {% endifequal %}
{% endfor %}