zoukankan      html  css  js  c++  java
  • 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>
    

    分页效果

    分页.jpg

  • 相关阅读:
    BZOJ 1251 序列终结者(Splay)
    ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
    POJ 2887 Big String(块状链表)
    BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)
    Codeforces Beta Round #13 C. Sequence (DP)
    Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
    MemSQL start[c]up Round 2
    Codeforces Round #195 A B C 三题合集 (Div. 2)
    哈密尔顿回路总结
    最大团问题
  • 原文地址:https://www.cnblogs.com/guigujun/p/8608679.html
Copyright © 2011-2022 走看看