【问题标题】:Django pagination not working. Object list is rendering items but not dividing into pagesDjango 分页不起作用。对象列表正在渲染项目但不分页
【发布时间】:2021-02-03 08:28:48
【问题描述】:

我目前正在开发一个电子商务网站,我正在尝试创建一个产品列表页面,该页面在显示 4 个项目后会跨越另一个页面。渲染页面不会产生任何错误,但数据库中的所有项目都显示在同一页面上,并且即使切换到另一个页面后也保持不变。

这是我的观点.py:


from django.shortcuts import render, get_object_or_404
from .models import Category, Item
from django.core.paginator import Paginator   


def item_list(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    items = Item.objects.filter(available=True)

    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        items = items.filter(category=category)

    paginator = Paginator(items, 4)
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    return render(request,
                  'item/list.html',
                  {'category': category,
                   'categories': categories,
                   'page_obj': page_obj,
                   'items': items})

我的 urls.py 文件:

from django.urls import path
from . import views

app_name = 'shop'

urlpatterns = [
    path('', views.item_list, name='item_list'),
    path('<slug:category_slug>/', views.item_list, name='item_list'),
    path('<int:id>/<slug:slug>/', views.item_detail, name='item_detail'),
]

模板中的分页sn-p

<nav class="d-flex justify-content-center wow fadeIn">
        <ul class="pagination pg-blue">

          <!--Arrow left-->
          {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}">Previous</a>
          {% endif %}
            <span class="current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
            </span>
          {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">Next</a>
          {% endif %}
        </ul>
 </nav>

以及展示产品的逻辑

<div class="row wow fadeIn">

          <!--Grid column-->
          {% for item in items %}
          <div class="col-lg-3 col-md-6 mb-4">

            <!--Card-->
            <div class="card" style="width: 16rem; height: 25rem">

              <!--Card image-->
              <div class="view overlay">
                <a href="{{ item.get_absolute_url }}">
                  {% if item.tags and item.label %}
                  <div class="imgHolder">
                    <img src="{{ item.image.url }}" class="img-fluid">
                    <span class="badge badge-pill {{ item.get_label_display }}-color">{{ item.tags }}</span>
                  </div>
                  {% else %}
                  <img src="{{ item.image.url }}" class="img-fluid">
                  {% endif %}
                </a>
                <a href="{{ item.get_absolute_url }}">
                  <div class="mask rgba-white-slight"></div>
                </a>
              </div>
              <!--Card image-->

              <!--Card content-->
              <div class="card-body text-center" style="height: 9rem;">
                <!--Category & Title-->
                <a href="{{ item.category.get_absolute_url }}" class="grey-text">
                  <h5>{{ item.category.name|truncatechars:20 }}</h5>
                </a>
                <h5>
                  <strong style="color:red">
                    <a href="{{ item.get_absolute_url }}">{{ item.name }}</a>
                  </strong>
                </h5>

                <h6 class="font-weight-bold">
                  {% if item.discount_price %}
                  <strong><del>${{ item.price}}</del></strong>
                  <strong>${{ item.discount_price }}</strong>
                  {% else %}
                  <strong>${{ item.price }}</strong>
                  {% endif %}
                </h6>

              </div>
              <!--Card content-->

            </div>
            <!--Card-->
          </div>
          {% endfor %}
        </div>

我真的很想知道我做错了什么,并且我也愿意接受解决同一问题的其他方法。谢谢

【问题讨论】:

    标签: python django pagination


    【解决方案1】:

    您正在使用您传递给分页器的所有查询集。您必须遍历 page_obj.object_list。

    page_obj = paginator.get_page(page_number)
    page_obj_items = page_obj.object_list
    

    【讨论】:

      猜你喜欢
      • 2016-08-22
      • 2011-03-04
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2019-05-28
      • 1970-01-01
      相关资源
      最近更新 更多