django-pure-pagination实现分页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django-pure-pagination实现分页相关的知识,希望对你有一定的参考价值。

django-pure-paginations是一个第三方的分页插件

安装 django-pure-pagination

pip install django-pure-pagination

在settings里的INSTALLED_APPS下新增如下

INSTALLED_APPS = [
    ‘pure_pagination‘,
]

在views中使用

#引入
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

class UserHistoryView(LoginRequiredMixin,ListView):
    ‘‘‘登录日志‘‘‘
    queryset = UserLog.objects.all().order_by(‘-login_time‘)
    template_name = ‘users/user_history.html‘
    # context_object_name = ‘user_history‘

    def get_context_data(self, **kwargs):
   #分页开始
        try:
            page = self.request.GET.get(‘page‘, 1)
        except PageNotAnInteger:
            page = 1
            # 这里指从all中取10个出来,每页显示10个
        p = Paginator(self.queryset, 10, request=self.request)
        page_list = p.page(page)
        print(page_list)
        context = {
            "platform_active": "active",
            "user_log_active": "active",
            #返回给模板
            "page_list":page_list,
        }
        kwargs.update(context)
        return super(UserHistoryView, self).get_context_data(**kwargs)

模板中使用

       <div class="table-responsive">

                                <form id="del_form_asset_all" class="form-horizontal  ">
                                    <table class="table table-striped table-bordered table-hover dataTables-example">
                                        <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>用户</th>
                                            <th>客户端</th>
                                            <th>来源IP</th>
                                            <th>城市</th>
                                            <th>登录时间</th>

                                        </tr>
                                        </thead>
                                        <tbody>

                                        {% for user_history in  page_list.object_list %}

                                            <tr class="gradeA">
                                                <td>{{ user_history.id }}</td>
                                                <td>{{ user_history.username }}</td>
                                                <td>{{ user_history.user_agent }}</td>
                                                <td>{{ user_history.ip }}</td>
                                                <td>{{ user_history.city }}</td>
                                                <td>{{ user_history.login_time }}</td>

                                            </tr>
                                        {% endfor %}

                                        </tbody>

                                    </table>
                                </form>
                              #分页开始
                                <div>
                                    <ul class="pagination pull-right">
                                        {% if page_list.has_previous %}
                                            <li class="long"><a
                                                    href="?{{ page_list.previous_page_number.querystring }}">上一页</a>
                                            </li>
                                        {% endif %}
                                        {% for page in page_list.pages %}
                                            {% if page %}
                                                {% ifequal page page_list.number %}
                                                    <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a>
                                                    </li>
                                                {% else %}
                                                    <li><a href="?{{ page.querystring }}">{{ page }}</a>
                                                {% endifequal %}
                                            {% else %}
                                                <li class="none"><a href="">...</a></li>
                                            {% endif %}
                                        {% endfor %}
                                        {% if page_list.has_next %}
                                            <li class="long"><a
                                                    href="?{{ page_list.next_page_number.querystring }}">下一页</a></li>
                                        {% endif %}
                                    </ul>

                                </div>

                            </div>

分页效果

技术分享图片

以上是关于django-pure-pagination实现分页的主要内容,如果未能解决你的问题,请参考以下文章

django实现分页功能

django实现分页(pure-pagination)

django 分页django-pure-pagination

[py][mx]django分页第三方模块django-pure-pagination

django-pure-pagination使用方法

DEDEcms 内容页如何实现内容分页