Django中ListView分页技术

2019-07-10 16:06:34 浏览数 (1)

Django中分页非常方便,通过Pagination你可以很方便达到分页效果。今天主要说的是基础视图中ListView的分页处理,本质还是依赖与Pagination。

数据模型:models.py

代码语言:javascript复制
    from django.db import models
    from django.contrib.auth.models import User
    # Create your models here.
    class Msg(models.Model):
        title = models.CharField(max_length = 30)
        content = models.TextField()
        user = models.ForeignKey(User)
        ip = models.IPAddressField()
        datetime = models.DateTimeField(auto_now_add = True)
        click_count = models.IntegerField(default = 0)

views.py

代码语言:javascript复制
 from django.views.generic import ListView
 from msg_board.models import Msg
 ITEMS_PER_PAGE = 1
          
 class MsgList(ListView):
     model = Msg#数据模型
     context_object_name = 'msg_list'#模板中变量
     template_name = 'index.html'#模板文件
     paginate_by = ITEMS_PER_PAGE#一个页面显示的条目

urls.py

代码语言:javascript复制
urlpatterns = [ 
    re_path(r'^list/(?P<pk>[0-9] )$', views.MsgListView.as_view(), ),
]

模板:index.html

代码语言:javascript复制
<html>
    <head><title>msg_board</title>
    <style type="text/css">
    table{border:3 solid black; border-collapse:collapse}
    table th, table td{border:2 solid black}
    </style>
     
    </head>
    <body>
    <h3>Message:</h3>
    {% if msg_list %}
        <table id = "msgs">
            <tr>
                <th>Title</th>
                <th>Content</th>
                <th>Author</th>
                <th>Ip</th>
                <th>Time</th>
                <th>Click</th>
            </tr>
        {%for msg in msg_list %}
            <tr>
                <td>{{msg.title}}</td>
                <td>{{msg.content}}</td>
                <td>{{msg.user}}</td>
                <td>{{msg.ip}}</td>
                <td>{{msg.datetime}}</td>
                <td>{{msg.click_count}}</td>
            </tr>
        {% endfor%}
        </table>
        {% if is_paginated %}
        <div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="/mysite?page={{ page_obj.previous_page_number }}">上一页</a>
                {% endif %}
                {% if page_obj.has_next %}
                    <a href="/mysite?page={{ page_obj.next_page_number }}">下一页</a>
                {% endif %}
                <span class="page-current">
                    第{{ page_obj.number }}页 ,工{{ page_obj.paginator.num_pages }}页。
                </span>
            </span>
        </div>
        {%endif%}
    {% else %}
    <p>No msgs !!!</p>
    {% endif %}
    </body>
    </html>
或者
<html>
    <head><title>msg_board</title>
    <style type="text/css">
    table{border:3 solid black; border-collapse:collapse}
    table th, table td{border:2 solid black}
    </style>
     
    </head>
    <body>
    <h3>Message:</h3>
    {% if msg_list %}
        <table id = "msgs">
            <tr>
                <th>Title</th>
                <th>Content</th>
                <th>Author</th>
                <th>Ip</th>
                <th>Time</th>
                <th>Click</th>
            </tr>
        {%for msg in msg_list %}
            <tr>
                <td>{{msg.title}}</td>
                <td>{{msg.content}}</td>
                <td>{{msg.user}}</td>
                <td>{{msg.ip}}</td>
                <td>{{msg.datetime}}</td>
                <td>{{msg.click_count}}</td>
            </tr>
        {% endfor%}
        </table>

{% if is_paginated %}
                    <nav aria-label="...">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                            <li class="page-item ">
                                <a class="page-link" href="{{ request.path }}?page=1" tabindex="-1" aria-label="Previous">
                                <span aria-hidden="true"  >&laquo;</span>
                                <span class="sr-only">Previous</span>
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item disabled">
                                <a  class="page-link" href="{{ request.path }}?page=1" > <span aria-hidden="true">&laquo;</span></a>
                            </li>
                            {% endif %}
                            {% for i in page_obj.paginator.page_range %}
                                {% if page_obj.number == i %}
                                    <li class="page-item active">
                                        <a class="page-link"  href="{{ request.path }}?age={{ i }}">{{ i }}</a>
                                    </li>
                                {% else %}
                                    <li class="page-item">
                                        <a class="page-link"  href="{{ request.path }}?page={{ i }}">{{ i }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            <li class="page-item">
                                <a  class="page-link"> 共{{ page_obj.paginator.num_pages }}页</a>
                            </li>
                            {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link"  href="{{ request.path }}?page={{ page_obj.next_page_number }}"><span aria-hidden="true">&raquo;</span></a></li>
                        {% else %}
                        <li class="page-item" aria-label="Next">
                            <a  class="page-link"> <span aria-hidden="true"  >&raquo;</span></a>
                        <span class="sr-only">Next</span>
                        </li>
                        {% endif %}
                        </ul>
                    </nav>
                {%endif%}
    {% else %}
    <p>No msgs !!!</p>
    {% endif %}
    </body>
    </html>

效果图:

page_obj是分页对像实例,这个名字是ListView默认生成的

page_obj的变量:

page_obj.paginator.page_range:所有分页的number,是一个list

page_obj.number:当前分页的页码

page_obj.paginator.num_pages:总分页数,就是一共有多少页

page_obj的方法:

page_obj.has_previous():判断是否有上一页

page_obj.previous_page_number():返回上一页的number

page_obj.has_next():判断是否有下一页

page_obj.next_page_number():返回下一页的number

0 人点赞