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="?page=1">第一页</a></li>
            {% endif %}

            {% if current_page.number == 1 %}
                <li><a href="?page={{ current_page.number }}">1</a></li>
            {% endif %}

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

            {% if current_page.number|add:2 <= current_page.paginator.num_pages %}
                <li><a href="?page={{ current_page.next_page_number }}">{{ current_page.next_page_number }}</a></li>
                <li><a href="?page={{ current_page.number|add:2 }}">{{ current_page.number|add:2 }}</a></li>
                <li><a href="?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="?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>
时间: 2024-11-12 05:15:15

Django个性化分页的相关文章

django 自定义分页模块

django 自定义分页模块 from django.shortcuts import render, HttpResponse, redirect from django.utils.safestring import mark_safe class Page(object): def __init__(self, current_page): self.current_page = int(current_page) @property def start(self): return (se

django orm 分页(paginator)取数据出现警告manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: &lt;class &#39;sign.models.Guest&#39;&gt; QuerySet.

使用django的orm做分页(Paginator)时出现了下面的警告 In [19]: p=Paginator(guest_list,2) manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'sign.models.Guest'> QuerySet. #!/usr/bin/env python 网上搜的

Django html 分页

16.分页 django 自带的分页:django paginator 参考:https://docs.djangoproject.com/en/1.10/topics/pagination/ >>> from django.core.paginator import Paginator >>> objects = ['john', 'paul', 'george', 'ringo'] >>> p = Paginator(objects, 2) >

Django - Ajax分页

目前总结了2种方法: 1. Ajax 分页 尼玛各种google,stackoverflow,搞了好久才总结出这个,之前使用Pagination tag loading的方式不好用,并且不能进行ajax提交请求的页面无刷新的方式去分页 1.view.py 1 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 2 from django.shortcuts import render 3 def xxx

django之分页

分页 在当前网页如果特别的数据,如果不想当前网页显示太多的内容数据(例如一些文章的目录或者一些小说的章节目录很多地方都用到分页),这时候就需要用分页了. 在Django中数据被分在不同页面中,并带有“上一页/下一页”标签.这些类位于django/core/paginator.py中. 分页器Paginator Paginator.py源码 import collections from math import ceil from django.utils import six from djan

django的分页--不全也未实现

一.Django内置分页 Paginator 二.自定义分页 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3.根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置 4.在数据表中根据起始位置取值,页面上输出数据 需求又来了,需要在页面上显示分页的页面.如:[上一页][1][2][3][4][5][下一页] 1.设定每页显示数据条数 2.用户输入页码(第一页.第二

django之分页插件

1 from django.utils.safestring import mark_safe 2 3 4 class Page: 5 def __init__(self, current_page, data_count, per_page_count=2, pager_num=7): 6 self.current_page = current_page 7 self.data_count = data_count 8 self.per_page_count = per_page_count

django 实现分页功能

分页效果: 视图代码: 1 # -*- coding: utf-8 -*- 2 from django.shortcuts import render,get_object_or_404 3 from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage 4 5 from .models import Article 6 7 # Create your views here. 8 9 def index(request

django之分页、cookie装饰器

一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7): self.current_page = current_page self.data_count = data_count self.per_page_count = per_page_count self.p