首页 > 代码库 > Django个性化分页

Django个性化分页

直接上代码吧:


# views.py
def accesslog(request):
    state = login_status(request.user.is_authenticated())
    log_list = AccessLog.objects.all().order_by()
    paginator = Paginator(log_list, 10)
    current_page_num = request.GET.get(‘page‘, 1)
    try:
        current_page = paginator.page(current_page_num)
    except EmptyPage:
        current_page = paginator.page(paginator.num_pages)
    except PageNotAnInteger:
        current_page = paginator.page(1)

    return render(request, ‘radius/accesslog.html‘, {
            ‘current_page‘: current_page,
            ‘paginator‘: paginator,
            ‘state‘: state
    })
    
# templatetags/mytag.py

from django import template
register = template.Library()

@register.filter(name=‘substra‘)
def substra(value, arg):
   try:
      return int(value) - int(arg)
   except:
      return ‘‘

HTML

# accesslog.html
{% load mytag %}
    <div class="container">
        <table class="table table-condensed">
            {% for log in current_page.object_list %}
                <tr>
                    <td>{{ log.id }}</td>
                    <td>{{ log.username }}</td>
                    <td>{{ log.ipaddr }}</td>
                    <td>{{ log.access_datetime|date:"Y-m-d H:s" }}</td>
                    {% if log.status %}
                        <td><span class="label label-success">认证成功</span></td>
                    {% else %}
                        <td><span class="label label-danger">认证失败</span></td>
                    {% endif %}
                    <td>{{ log.message }}</td>
                </tr>
            {% endfor %}

        </table>

    <nav>
{#    {{ current_page.paginator.num_pages }}#}
        <ul class="pagination">
            {% if current_page.number > 3 %}
                <li ><a href="http://www.mamicode.com/?page=1">第一页</a></li>
            {% endif %}

            {% if current_page.number == 1 %}
                <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">1</a></li>
            {% endif %}

            {% if current_page.number > 2 %}
                <li><a href="http://www.mamicode.com/?page={{ current_page.number|substra:2 }}">{{ current_page.number|substra:2 }}</a></li>
                <li><a href="http://www.mamicode.com/?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li>
                <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">{{ current_page.number }}</a></li>
            {% elif current_page.number == 2 %}
                <li><a href="http://www.mamicode.com/?page={{ current_page.previous_page_number }}">{{ current_page.previous_page_number }}</a></li>
                <li><a href="http://www.mamicode.com/?page={{ current_page.number }}">{{ current_page.number }}</a></li>
            {% endif %}

            {% if current_page.number|add:2 <= current_page.paginator.num_pages %}
                <li><a href="http://www.mamicode.com/?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li>
                <li><a href="http://www.mamicode.com/?page={{ current_page.number|add:2 }}">{{ current_page.number|add:2 }}</a></li>
                <li><a href="http://www.mamicode.com/?page={{ current_page.paginator.num_pages }}">最后一页</a></li>
            {% elif current_page.number == current_page.paginator.num_pages %}
            {# nothing to do#}
            {% elif current_page.number|add:2 >= current_page.paginator.num_pages %}
                <li><a href="http://www.mamicode.com/?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li>
            {% endif %}



        </ul>
    </nav>

    </div>
    </div>
{% include ‘radius/footer.html‘ %}

<script>
    $(document).ready(function(){
        $(‘.pagination li a‘).each(function(){
           if ( $(this).html() == {{ current_page.number }} ){
               $(this).parent().addClass(‘active‘)
           }
        });
    })
</script>


本文出自 “jun.wang” 博客,请务必保留此出处http://junwang.blog.51cto.com/5050337/1876819

Django个性化分页