曦远 (DealiAxy)一份私人的文字刊物

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项目引入NPM和gulp管理前端资源

前言 之前写了一篇《Asp-Net-Core开发笔记:使用NPM和gulp管理前端静态文件》,现在又来用Django开发项目了,之前我搞了一个Django的快速开发脚手架「DjangoStarter」,正好给升级一下~ 关于npm和gulp我就不多重复了,之前那篇文章都写了,直接上操作 安装依赖 安装依赖的操作跟上一篇文章是一样的,不过我这里直接提供package.json文件,复制到项目根目录里

2022-04-02 · Django · 约 1 分钟

Django中间件之实现Admin后台IP白名单

前言 Django自带的Admin管理后台很方便,但是实际生产环境真的会有挺多安全问题的,在admin的安全防护这方面,我之前就研究实现了给admin加上登录验证码和限流功能,可以参考这篇文章:给Django Admin添加验证码和多次登录尝试限制 不过就在内部使用的话就好很多,因此我们可以通过Django的中间件来实现IP过滤,所以本文中涉及到两个知识点,一个是Django的中间件,另外一个是P

2022-08-26 · Django · 约 1 分钟

讨论

共 0 条

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

加载评论中…