zoukankan      html  css  js  c++  java
  • django之分页

    一、导入

    from django.core.paginator import Paginator, InvalidPage

    二、使用

    def list_article(request):
        username = request.session.get("username")
        if username:
            # articles = Article.objects.filter(author__username=username)
            categorys = Category.objects.filter(author__username=username)
            # 自定义文章列表
            articles = []
            # 生成1000篇文章
            for i in range(1000):
                article = dict(id=i, title="Python从入门到放弃", category="Python",
                               author="Will Smith", created_time="2019-12-21",
                               latest_edit="2020-02-23")
                articles.append(article)
            # 获取请求页码
            page_num = int(request.GET.get("p", 1))
            # 用文章列表和每页显示的文章数初始化分页器,获取一个分页器对象
            paginator = Paginator(articles, 10)
            # 分页器的四个主要属性:count(传入的对象数量)、num_pages(分的页数)、page_range(页码的范围)、per_page(每页显示的对象数量)
            print(paginator.count, paginator.per_page, paginator.num_pages)
            try:
                # 分页器的一个主要方法get_page(传入页码,获取对应的页对象)
                # 考虑几种情形:1、页码不为整数即无效页码 2、页码小于或超出实际页码范围
                page_articles = paginator.page(number=page_num)
            except InvalidPage as e:
                page_articles = paginator.page(1)
            # 将页对象传递给模板进行渲染,页对象是一个可以迭代的对象,获取的是该页对象列表
            # 页对象有几个主要的方法和属性:
            # object_list 当前页的内容,元素
            # number 当前页的页码
            # paginator 分页器对象
            # has_next 有没有下一页
            # next_page_number 下一页的页码
            # has_previous 有没有上一页
            # previous_page_number 上一页页码
            # has_other_page 当前页有没有上一个或者下一页
            # start_index 当前页第一个对象的索引
            # end_index 当前页最后一个对象的索引
            return render(request, "article/list-article.html", {"articles": page_articles, "categorys": categorys})
    <div id="list-article">
            <table class="table table-bordered table-striped table-hover table-condensed text-center">
                <tr>
                    <th class="text-center">文章ID</th>
                    <th class="text-center">文章标题</th>
                    <th class="text-center">所属分类</th>
                    <th class="text-center">作者</th>
                    <th class="text-center">创建时间</th>
                    <th class="text-center">修改时间</th>
                    <th class="text-center">操作</th>
                </tr>
                {% for article in articles %}
                <tr>
                    <td>{{ article.id }}</td>
                    <td>{{ article.title }}</td>
                    <td>{{ article.category}}</td>
                    <td>{{ article.author }}</td>
                    <td>{{ article.created_time }}</td>
                    <td>{{ article.latest_edit }}</td>
                    <td><a href="{% url 'article:edit' article.id %}">编辑</a>&nbsp;&nbsp;
                        &nbsp;
                        &nbsp;<a href="{% url 'article:delete' article.id %}">删除</a></td>
                </tr>
                {% endfor %}
            </table>
    
            <nav>
                <ul class="pagination pagination pull-right">
                    {% if articles.has_previous %}
                        <li><a href="{% url 'article:list' %}?p={{ articles.previous_page_number }}"><span>&laquo;</span></a></li>
                    {% else %}
                        <li class="disabled"><a href="{% url 'article:list' %}?p=1"><span>&laquo;</span></a></li>
                    {% endif %}
                    {% for page in articles.paginator.page_range %}
                        {% if page > articles.number|add:-5 and page < articles.number|add:5 %}
                            {% if page == articles.number %}
                                <li class="active"><a href="{% url 'article:list' %}?p={{ page }}">{{ page }}</a></li>
                            {% else %}
                                <li><a href="{% url 'article:list' %}?p={{ page }}">{{ page }}</a></li>
                            {% endif %}
    
                        {% endif %}
                    {% endfor %}
                    {% if articles.has_next %}
                        <li><a href="{% url 'article:list' %}?p={{ articles.next_page_number }}"><span>&raquo;</span></a></li>
                    {% else %}
                        <li class="disabled"><a href="{% url 'article:list' %}?p={{ articles.number }}"><span>&raquo;</span></a></li>
                    {% endif %}
                </ul>
            </nav>
        </div>

  • 相关阅读:
    RocketMQ性能压测分析(转载)
    利用Fiddler或Charles进行mock数据
    Linux中buffer/cache,swap,虚拟内存和page ++
    AVA 8 :从永久区(PermGen)到元空间(Metaspace)
    jstat 监控调整GC很好用
    Jmeter常用函数
    关于Oracle新建表空间,添加用户及新建表数据
    关于Oracle增加表空间大小方法
    关于手动删除Oracle数据数据,导致Oracle无法连接处理过程
    解决jquery easyui combotree(下拉树)点击文字无法展开下级菜单的解决方法
  • 原文地址:https://www.cnblogs.com/loveprogramme/p/12493877.html
Copyright © 2011-2022 走看看