• django-paginator


    py code...

    from django.core.paginator import Paginator
    
    
    class NewsListView(View):
        def get(self, request):
            '''分页'''
            page = int(request.GET.get('page', 1))  # 第多少页,默认1
            print(page)
            newses = News.objects.select_related("category", "author").all()
            categories = NewsCategory.objects.all()
    
            p = Paginator(newses, 5)  # 每页显示n条数据
            page_obj = p.page(page)  # 第n页的 page对象 <Page 1 of n>
    
            current_page_newses = page_obj.object_list  # not func!! 当前页的newes所有数据 设置的是显示一条data
            context = {
                'newses': page_obj.object_list,
                'categories': categories,
                'p': p,
                'current_page': page
            }
            context.update(self.get_paging_data(p , page_obj, show_pages=2, page=page))
            return render(request, 'cms/news_list.html', context=context)
    
        def get_paging_data(self, paginator, page_obj, show_pages,page=None):
            """
            分页当前页的左右范围
            :param paginator: Paginator(obj, number)对象
            :param page_obj: # 第n页的 page对象 <Page 1 of n> page_obj.number获取当前页
            :param show_pages: 当前页左右展示的页数
            :param page: 当前页  ps:可选参数
            :return: 当前页左右显示的页数range对象 和 has_left(right)_more Bool值,用来判断是否显示 分页的省略号 ...
            """
            current_page = page_obj.number
            show_pages = show_pages
            pages_number = paginator.num_pages
    
            has_left_more = False
            has_right_more = False
            # 左边的逻辑判断
            print(current_page, 'c')
            if current_page <= show_pages + 2:
                left_start = 1
                left_end = current_page
            else:
                has_left_more = True
                left_start = current_page - show_pages
                left_end = current_page
            left_range = range(left_start, left_end)
            # 右边的逻辑判断(在右边包括了当前页)
            if current_page >= pages_number - show_pages - 1:
                right_start = current_page
                right_end = pages_number + 1
            else:
                has_right_more = True
                right_start = current_page
                right_end = current_page + show_pages + 1
            right_range = range(right_start, right_end)
            data = {
                'left_range': left_range,
                'right_range': right_range,
                'has_left_more': has_left_more,
                'has_right_more': has_right_more,
                'current_page': current_page,
            }
            print(left_range, has_left_more)
            return data

     html code

        <link rel="stylesheet" href="{% static 'adminlte/bower_components/bootstrap/dist/css/bootstrap.min.css' %}">
        <link rel="stylesheet" href="{% static 'adminlte/bower_components/font-awesome/css/font-awesome.min.css' %}">
        <link rel="stylesheet" href="{% static 'adminlte/dist/css/AdminLTE.min.css' %}">
        <link rel="stylesheet" href="{% static 'adminlte/dist/css/skins/_all-skins.min.css' %}">
        <link rel="stylesheet" href="{% static 'sweetalert/sweetalert.css' %}">
        <script src="{% static 'adminlte/bower_components/jquery/dist/jquery.min.js' %}"></script>
        <script src="{% static 'adminlte/bower_components/bootstrap/dist/js/bootstrap.min.js' %}"></script>
        <script src="{% static 'adminlte/dist/js/adminlte.min.js' %}"></script>
        <script src="{% static 'sweetalert/sweetalert.min.js' %}"></script>
        <script src="{% static 'sweetalert/xfzalert.js' %}"></script>
        <script src="{% static 'js/xfzajax.js' %}"></script>
        <script src="{% static 'js/message.js' %}"></script>
    {% block content %}
        <div class="box">
            <div class="box-header">
                <form class="form-inline">
                    <div class="form-group">
                        <div class="pull-left margins">
                            <label for="start-input">时间:</label>
                            <input readonly class="form-control" type="text" id="start-input" placeholder="开始时间">
                            <span>-</span>
                            <input readonly class="form-control" type="text" placeholder="结束时间">
                        </div>
                    </div>
                    <div class="form-group margins">
                        <label for="title-input">标题:</label>
                        <input class="form-control" type="text" id="title-input" placeholder="关键字">
                    </div>
                    <div class="form-group margins">
                        <label for="">分类:</label>
                        <select class="form-control" name="" id="category-input">
                            {% for category in categories %}
                                <option value="">{{ category.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="form-group margins">
                        <button class="btn btn-primary btn-sm clear-btn">查询</button>
                    </div>
                    <div class="form-group margins">
                        <a href="{% url 'cms:news_list' %}" class="btn btn-info btn-sm">清除查询</a>
                    </div>
                </form>
            </div>
            <div class="box-body">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>标题</th>
                        <th>分类</th>
                        <th>发布时间</th>
                        <th>作者</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for news in newses %}
                        <tr>
                            <td>
                                <a href="{% url 'news:news_detail' news_id=news.pk %}">{{ news.title }}</a>
                            </td>
                            <td>{{ news.category.name }}</td>
                            <td>{{ news.pubtime | cms_date_filter }}</td>
                            <td>{{ news.author.username }}</td>
                            <td>
                                <button class="btn btn-warning btn-xs">编辑</button>
                                <button class="btn btn-danger btn-xs">删除</button>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="box-footer">
                <span class="pull-left">第{{ current_page }}页/共{{ p.num_pages }}页</span>
                <nav aria-label="Page navigation" class="pull-right">
                    <ul class="pagination">
                        {# 前一页 #}
                        {% if page_obj.has_previous %}
                            <li>
                                <a href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <span aria-hidden="true">&laquo;</span>
                            </li>
                        {% endif %}
    
                        {# 当前页左边部分 #}
                        {% if has_left_more %}
                            <li><a href="?page=1">1</a></li>
                            <li><span>...</span></li>
                            {% for page in left_range %}
                                <li><a href="?page={{ page }}">{{ page }}</a></li>
                            {% endfor %}
                        {% else %}
                            {% for page in left_range %}
                                <li><a href="?page={{ page }}">{{ page }}</a></li>
                            {% endfor %}
                        {% endif %}
                        {# 当前页右边部分 ,包括了当前页 #}
                        {% if has_right_more %}
                            {% for page in right_range %}
                                <li><a href="?page={{ page }}">{{ page }}</a></li>
                            {% endfor %}
                            <li><span>...</span></li>
                            <li><a href="?page={{ p.num_pages }}">{{ p.num_pages }}</a></li>
                        {% else %}
                            {% for page in right_range %}
                                <li><a href="?page={{ page }}">{{ page }}</a></li>
                            {% endfor %}
                        {% endif %}
    
                        {# 后一页 #}
                        {% if page_obj.has_next %}
                            <li>
                                <a href="?page={{ page_obj.next_page_number }}" aria-label="Next" class="disabled">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <span aria-hidden="true">&raquo;</span>
                            </li>
                        {% endif %}
    
                    </ul>
                </nav>
            </div>
        </div>
    {% endblock %}
  • 相关阅读:
    Intel x86
    FPGA自计数六位共阳极数码管动态显示2(调用task的方法)
    FPGA六位共阳极数码管动态显示
    运算放大器是模拟电路的基石,模拟电路设计
    这样讲你就懂了!大牛给你介绍《信号与系统》
    电容计算公式
    fork...join的用法
    芯片电源管脚的去耦电容究竟要用多大的?
    Blog Contents
    linux grep 命令常见用法
  • 原文地址:https://www.cnblogs.com/tangpg/p/9367526.html
Copyright © 2020-2023  润新知