Python + Django4 to build a personal blog (17): article pagination

If there are a lot of blog posts, there may be a large number of articles according to the current article list, which will not only affect the appearance, but also affect the running speed of the website.

At this time we need to perform paging processing.

Django also thoughtfully prepared wheels for us in advance, and we can install them and use them.

We use Paginatorclasses for article pagination.

Class Paginator Description

The constructor of the Paginator class is:

class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)

1. Class parameters

The required parameters are:

  • object_list: Paginated list object
  • per_page: Maximum number of entries per page

Optional parameters:

  • orphans=0: When you use this parameter it means you don't want the last page to have very few entries. If the number of entries on the last page is less than or equal to the value of orphans, these entries will be merged into the previous page (the previous page becomes the last page at this time). For example, if there are 23 entries, per_page=10, orphans=0, then there are 3 pages, namely 10, 10, and 3. If orphans>=3, there are 2 pages, respectively 10, 13.
  • allow_empty_first_page=True: The first page is allowed to be empty by default.

2. Class method:

  • Paginator.page(number): Returns a Page object according to the parameter number. (number is a multiple of 1)

3. Type:

  • Paginator.count: The total number of objects on all pages, that is, the number of items in the object_list is counted. When counting object_listthe number of contained objects, Paginator will be called first object_list.count(). If object_listthere is no count()method, Paginator it will fall back to use len(object_list).
  • Pagnator.num_pages:Total number of pages.
  • pagiator.page_range: Page range, starting from 1, such as [1,2,3,4].

Using Paginator in the view

To paginate the article list, you need to modify article/views.pythe def article_list()view:

# 引入分页模块
from django.core.paginator import Paginator
def article_list(request):
    # # 取出所有博客文章
    # articles = Article.objects.all()
    # 修改变量名称(articles -> article_list)
    article_list = Article.objects.all()

    # 每页显示 1 篇文章
    paginator = Paginator(article_list, 1)
    # 获取 url 中的页码
    page = request.GET.get('page')
    # 将导航对象相应的页码内容返回给 articles
    articles = paginator.get_page(page)

    # 需要传递给模板(templates)的对象
    context = {'articles': articles}
    # render函数:载入模板,并返回context对象
    return render(request, 'article/list.html', context)

In the view Paginator, the content passed to the template is manipulated through the class:

What is returned is no longer a collection of all articles, but an object of some articles corresponding to the page number, and this object also contains paging methods.

We have already come across some methods of passing parameters to views in previous articles:

  • POSTPass form data to view via request
  • By urlpassing the parameters in the address to the view

Another method is used here: in GETthe request, the key-value pair is attached at the end of the url , and the value ?key=valuecan be queried in the view .request.GET.get('key')

Then rewrite the template and add paging content before the </div> at the end:

<!-- 页码导航 -->
<div class="pagination row">
    <div class="m-auto">
        <span class="step-links">
            <!-- 如果不是第一页,则显示上翻按钮 -->
            {% if articles.has_previous %}
                <span>...</span>
                <a href="?page={
   
   { articles.previous_page_number }}"
                   class="btn btn-secondary"
                >
                    {
   
   { articles.previous_page_number }}
                </a>
            {% endif %}

            <!-- 当前页面 -->
            <span class="current btn btn-danger btn-lg">
                {
   
   { articles.number }}
            </span>
            <!-- 如果不是最末页,则显示下翻按钮 -->
            {% if articles.has_next %}
                <a href="?page={
   
   { articles.next_page_number }}&order={
   
   { order }}"
                    class="btn btn-secondary">{
   
   { articles.next_page_number }}</a>
                <span>...</span>
                <a href="?page={
   
   { articles.paginator.num_pages }}&order={
   
   { order }}"
                    class="btn btn-success">{
   
   { articles.paginator.num_pages }} &raquo;</a>
            {% endif %}
        </span>
    </div>
</div>

Run the server and successfully implement the paging function.

Guess you like

Origin blog.csdn.net/agelee/article/details/127221817