返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Django

A Better Pagination Component Than Django's Official Implementation + Bootstrap Integration

Preface The pagination component that comes with Django can only be said to fulfill the pagination function, but it's just not that easy to use Django's pagination effect django-pure-pagination pagination effect Usage First, install: pip install django-pure-pagination Add to App INSTALLED_APPS = ( ... 'pure_pagination', ) Configure it; the configuration instructions are as shown in the figure P

曦远 (DealiAxy)约 12 分钟
目录10 个章节

Preface

The pagination component that comes with Django can only be said to fulfill the pagination function, but it's just not that easy to use

Django's pagination effect

django-pure-pagination pagination effect

Usage

First, install:

pip install django-pure-pagination

Add to App

INSTALLED_APPS = (
    ...
    'pure_pagination',
)

Configure it; the configuration instructions are as shown in the figure

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

View code example

from django.shortcuts import render
from pure_pagination import Paginator
from .models import Video

def all_video(request):
    video_list = Video.objects.all()
    paginator_obj = Paginator(video_list, 10, request=request)

    ctx = {
        'page': paginator_obj.page(request.GET.get('page', 1)),
    }

    return render(request, 'video_list.html', context=ctx)

Template code

Note that the Paginator object of django-pure-pagination is a bit different from Django's built-in Paginator; they differ in usage.

Django's built-in Paginator is itself an iterator, so you can just for item in page, but django-pure-pagination is different—you have to for item in page.object_list. Mind this difference~

Therefore, django-pure-pagination needs to iterate over the objects on the page like this:

{% for video in page.object_list %}
    {% include "widgets/video_card.html" %}
{% empty %}
    <div class="alert alert-secondary" role="alert">
        视频列表获取为空!
    </div>
{% endfor %}

Paginator HTML code

It's best to write a separate paginator.html as a Django template component, then other pages can conveniently include it with {% includ "paginator.html" %}

{% load i18n %}

{% if page.paginator.num_pages > 1 %}
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">
            {% if page.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?{{ page.previous_page_number.querystring }}">&lsaquo;&lsaquo; {% trans "previous" %}</a>
                </li>
            {% else %}
                <li class="page-item disabled">
                    <span class="page-link">&lsaquo;&lsaquo; {% trans "previous" %}</span>
                </li>
            {% endif %}

            {% for p in page.pages %}
                {% if p %}
                    {% ifequal p page.number %}
                        <li class="page-item active">
                            <span class="page-link">{{ p }}</span>
                        </li>
                    {% else %}
                        <li class="page-item">
                            <a class="page-link" href="?{{ p.querystring }}">{{ p }}</a>
                        </li>
                    {% endifequal %}
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>
                {% endif %}
            {% endfor %}

            {% if page.has_next %}
                <li class="page-item">
                    <a href="?{{ page.next_page_number.querystring }}" class="page-link">{% trans "next" %} &rsaquo;&rsaquo;</a>
                </li>
            {% else %}
                <li class="page-item disabled">
                    <span class="page-link disabled">{% trans "next" %} &rsaquo;&rsaquo;</span>
                </li>
            {% endif %}
        </ul>
    </nav>
{% endif %}

Final result

References

Welcome to connect

Programming Lab focuses on exploring popular new internet technologies and practicing team agile development. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account “Programming Lab” to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

_EOF_

A Better Pagination Component Than Django's Official Implementation + Bootstrap Integration

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Django中「强行」使用MVVM设计模式

扫盲 首先带大家了解一下什么是MVVM模式: 什么是MVVM?MVVM是Model-View-ViewModel的缩写。 MVVM是MVC的增强版,实质上和MVC没有本质区别,只是代码的位置变动而已 从名字上看,MVVM比MVC架构中多了一个ViewModel,没错,就是这个ViewModel,他是MVVM相对于MVC改进的核心思想。在开发过程中,由于需求的变更或添加,项目的复杂度越来越高,代码量

2021-12-18 · Django · 约 1 分钟

Django应用部署:Docker容器化实践

在此之前,我一直用uwsgi+virtualenv+nginx方式进行应用部署,操作起来比较麻烦,而且依赖于服务器上的Python版本,服务的管理方面单纯uwsgi + pid算不上特别麻烦但总没有docker compose方便,无奈原本的服务器是32位系统的用不上docker,现在又增加了一个新服务器,所以赶紧把docker安排上~ 不得不说真的太简单太方便了,全程傻瓜式操作,也基本没像之前u

2021-12-18 · Django · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…