1. 程式人生 > >django-pure-pagination實現分頁

django-pure-pagination實現分頁

paginati

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實現分頁