You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django分页器失效问题:页码正确但每页显示全部数据

Fixing Your Django Pagination Issue

Hey there, I see exactly what's going on here—your pagination setup is almost right, but there's one small (but crucial) mistake in your template that's causing all items to show up on every page.

The Root Problem

In your 商品列表渲染代码, you're looping through all_dress instead of the paginated dresss variable you created in your view.

all_dress is the full, unfiltered queryset of all active items—so every time you load a page, you're rendering every single item instead of just the slice for the current page. Your pagination controls are working correctly (hence the correct page count), but the actual content isn't using the paginated data.

The Fix

Update your product list loop to use the dresss variable instead of all_dress:

<ul class="thumbnails" >
 {% for dress in dresss %} <!-- Changed from all_dress to dresss -->
 <li class="span4 pull-left" >
 <div class="thumbnail">
 <a class="zoomTool" href="{% url 'dress_details' dress.pk%}" title="add to cart"><span class="icon-search"></span> 毓乇囟 丕賱鬲賮丕氐賷賱</a>
 <a href="{% url 'dress_details' dress.pk%}"><img class="main" src="{{ dress.dress_image1.url }}" alt=""></a>
 <div class="caption">
 <h5> 賮爻鬲丕賳 {{ dress.dress_name }} </h5>
 <h4>
 <a class="defaultBtn" href="{% url 'dress_details' dress.pk%}" title="廿囟賮胤 賱賲卮丕賴丿丞 丕賱賮爻鬲丕賳"><span class="icon-zoom-in"></span></a>
 <span class="pull-left">{{ dress.dress_price }} 噩賳賷賴 </span>
 </h4>
 </div>
 </div>
 </li>
 {% endfor %}
</ul>

Why This Works

  • In your views.py, paginator.get_page(page) returns a Page object (dresss) that contains only the items for the current page (3 items, as you configured).
  • Your pagination controls already use dresss correctly (checking has_previous, number, etc.), so once you update the loop to use this variable, each page will only show the appropriate slice of data.

Quick Check

Just to confirm, your view code is set up correctly:

  • You're creating the paginator with all_dress (the full queryset)
  • You're fetching the current page with paginator.get_page(page)
  • You're passing both all_dress and dresss to the context—but only dresss should be used for the paginated list.

That's it! After making this change, your pagination should work as expected—each page will show 3 items, and the controls will navigate between them properly.

内容的提问来源于stack exchange,提问作者Ahmed Wagdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:07:11