Django html 分页

16.分页

django 自带的分页:django paginator

参考:https://docs.djangoproject.com/en/1.10/topics/pagination/

>>> from django.core.paginator import Paginator
>>> objects = [‘john‘, ‘paul‘, ‘george‘, ‘ringo‘]
>>> p = Paginator(objects, 2)

>>> p.count
4
>>> p.num_pages     ##一共多少页,也可说是最后一页的页数
2
>>> p.page_range
[1, 2]
>>>

>>> page1 = p.page(1)
>>> page1
<Page 1 of 2>
>>> page1.object_list
[‘john‘, ‘paul‘]

>>> page2 = p.page(2)
>>> page2.object_list
[‘george‘, ‘ringo‘]
>>> page2.has_next()
False
>>> page2.has_previous()
True
>>> page2.has_other_pages()
True
>>> page2.next_page_number()
Traceback (most recent call last):
...
EmptyPage: That page contains no results
>>> page2.previous_page_number()
1
>>> page2.start_index() # The 1-based index of the first item on this page
3
>>> page2.end_index() # The 1-based index of the last item on this page
4
>>> p.page(0)
Traceback (most recent call last):
...
EmptyPage: That page number is less than 1
>>> p.page(3)
Traceback (most recent call last):
...
EmptyPage: That page contains no results

###################################
###################################
###################################  后端处理
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render

def listing(request):
    contact_list = Contacts.objects.all()     ###把内容取出来,但不是真正取出去。
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

    page = request.GET.get(‘page‘)         ##前台说要去那一页,就提交到这
    try:
        contacts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.     ##如果页面不是一个整数,交付第一页
        contacts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        contacts = paginator.page(paginator.num_pages)         ##如果取的页数超过最大页数,就返回最后一页

    return render(request, ‘list.html‘, {‘contacts‘: contacts})    ##把获取到的页面返回到前台

###################################
###################################
###################################  前端处理

{% for contact in contacts %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br />
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}      ##判断后端传来的页数,有没有上一页
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}  ##判断后端传来的页数,有没有下一页
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}    ### ?page ,加上‘?‘问号。 就是一个get方法。
    </span>
</div>

###################################
###################################
###################################  前端处理,用bootstrap 实现, 作业:线上分页中的前后几页,而不是下面的全部页面都显示

                    <nav>
                      <ul class="pagination">
                      {% if articles.has_previous %}
                        <li>
                            <a href="?page={{ articles.previous_page_number }}" aria-label="Previous">
                                <span aria-hidden="true">
                                    &laquo;
                                </span>
                            </a>
                        </li>
                      {% else %}
                        <li>
                            <a href="#" aria-label="Previous">
                                <span aria-hidden="true">
                                    &laquo;
                                </span>
                            </a>
                        </li>

                      {% endif %}

                      {% for p_num in articles.paginator.page_range %}
                        {% if articles.number == p_num %}
                        <li class="active">
                            <a href="#">
                                {{ articles.number }}
                                <span class="sr-only">{{ articles.number }}
                                </span>
                            </a>
                        </li>
                        {% else %}
                        <li >
                            <a href="?page={{ p_num }}">
                                {{ p_num }}
                            </a>
                        </li>
                        {% endif %}
                       {% endfor %}
                        {% if articles.has_next %}
                        <li>
                            <a href="?page={{ articles.next_page_number }}" aria-label="Next">
                                <span aria-hidden="true">
                                    &raquo;
                                </span>
                            </a>
                        </li>
                        {% else %}
                        <li>
                            <a href="#" aria-label="Next">
                                <span aria-hidden="true">
                                    &raquo;
                                </span>
                            </a>
                        </li>
                      {% endif %}
                      </ul>
                    </nav>

下面这个实例,分页页面按钮数最多显示为3个!

实例:
后端:
def index(request):   ##分页
    articles_list = models.Article.objects.all().order_by(‘-publish_date‘)  ###把内容取出来,但不是真正取出去。
    paginator = Paginator(articles_list, 2) # Show 2 contacts per page

    page = request.GET.get(‘page‘)     ##前台说要去那一页,就提交到这
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.  ##如果页面不是一个整数,交付第一页
        articles = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        articles = paginator.page(paginator.num_pages) ##如果取的页数超过最大页数,就返回最后一页

    page_range = range(articles.number - 1 ,articles.number + 2)
    max_page = paginator.num_pages
    return render(request,‘index.html‘,{
        ‘articles‘: articles,
        ‘page_range‘: page_range,
        ‘max_page‘: max_page,
    })

前端:
<div class="pagination">
<ul class="pagination">
    {% if articles.has_previous %}
        <li><a href="?page={{ articles.previous_page_number }}">previous</a>
        </li>
    {% endif %}

    {% for p_num in page_range %}
        {% if 0 < p_num and p_num < max_page %}
            {% if articles.number == p_num %}
                <li class="active">
                <a href="#">
                {{ articles.number }}
                <span class="sr-only">{{ articles.number }}
                </span>
                </a>
                </li>
            {% else %}
                <li >
                <a href="?page={{ p_num }}">
                    {{ p_num }}
                </a>
                </li>
            {% endif %}
        {% endif %}
    {% endfor %}
    
    
    {% if articles.has_next %}
        <li><a href="?page={{ articles.next_page_number }}">next</a></li>
    {% endif %}
</ul>
</div>
时间: 2024-12-29 19:50:31

Django html 分页的相关文章

django 自定义分页模块

django 自定义分页模块 from django.shortcuts import render, HttpResponse, redirect from django.utils.safestring import mark_safe class Page(object): def __init__(self, current_page): self.current_page = int(current_page) @property def start(self): return (se

django orm 分页(paginator)取数据出现警告manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: &lt;class &#39;sign.models.Guest&#39;&gt; QuerySet.

使用django的orm做分页(Paginator)时出现了下面的警告 In [19]: p=Paginator(guest_list,2) manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'sign.models.Guest'> QuerySet. #!/usr/bin/env python 网上搜的

Django - Ajax分页

目前总结了2种方法: 1. Ajax 分页 尼玛各种google,stackoverflow,搞了好久才总结出这个,之前使用Pagination tag loading的方式不好用,并且不能进行ajax提交请求的页面无刷新的方式去分页 1.view.py 1 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 2 from django.shortcuts import render 3 def xxx

django之分页

分页 在当前网页如果特别的数据,如果不想当前网页显示太多的内容数据(例如一些文章的目录或者一些小说的章节目录很多地方都用到分页),这时候就需要用分页了. 在Django中数据被分在不同页面中,并带有“上一页/下一页”标签.这些类位于django/core/paginator.py中. 分页器Paginator Paginator.py源码 import collections from math import ceil from django.utils import six from djan

django的分页--不全也未实现

一.Django内置分页 Paginator 二.自定义分页 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3.根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置 4.在数据表中根据起始位置取值,页面上输出数据 需求又来了,需要在页面上显示分页的页面.如:[上一页][1][2][3][4][5][下一页] 1.设定每页显示数据条数 2.用户输入页码(第一页.第二

django之分页插件

1 from django.utils.safestring import mark_safe 2 3 4 class Page: 5 def __init__(self, current_page, data_count, per_page_count=2, pager_num=7): 6 self.current_page = current_page 7 self.data_count = data_count 8 self.per_page_count = per_page_count

django 实现分页功能

分页效果: 视图代码: 1 # -*- coding: utf-8 -*- 2 from django.shortcuts import render,get_object_or_404 3 from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage 4 5 from .models import Article 6 7 # Create your views here. 8 9 def index(request

django之分页、cookie装饰器

一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7): self.current_page = current_page self.data_count = data_count self.per_page_count = per_page_count self.p

Django自定义分页、bottle

一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1">1</a>,将page的值传送到函数/user_list/中,后端在user_list中通过request.GET.get('page',1)获取当前页; 3.从数据库中获取特定行的数据,使用result = models.UserList.objects.all()[start:end]获取,