paginator在django中不起作用

时间:2018-05-24 11:59:08

标签: python django

我已经跟随官方文档在Django中使用paginator并且它不起作用,它显示了正确的页面数,但是在每个页面上,整个列表显示而不是将其切成许多页面

views.py

def home(request):
    current_user = request.user
    all_dress = Item.objects.all().filter(dress_active=True).order_by('-created_at')
    all_good = Item.objects.all().filter(dress_special=True)
    all_name = Name.objects.all()
    all_ads = Ads.objects.all()
    #pig
    paginator = Paginator(all_dress, 3) # Show 12 dress per page
    page = request.GET.get('page')
    dresss = paginator.get_page(page)

    context = {
        'all_dress': all_dress,
        'all_name': all_name,
        'current_user': current_user,
        'all_good':all_good,
        'all_ads':all_ads,
        'dresss':dresss,
    }
    return render(request, 'fostan/index.html',context)

HTML

    <div class="row">
    <div class="col">
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    <div class="col">
        <div dir="ltr">
     <div class="pagination" align="left">
    <span class="step-links" align="left">
        {% if dresss.has_previous %}
            <a href="?page=1">&laquo; first </a>
            <a href="?page={{ dresss.previous_page_number }}"> previous</a>
        {% endif %}

        <span class="current">
            Page number {{ dresss.number }} of {{ dresss.paginator.num_pages }}
        </span>
<br>
        {% if dresss.has_next %}
            <a href="?page={{ dresss.next_page_number }}">next </a>
            <a href="?page={{ dresss.paginator.num_pages }}">last  &raquo;</a>
        {% endif %}
    </span>
</div>
    </div>
</div>
    <div class="col">
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>

我有9个项目,我要求分页器每页只显示3个项目,结果是3个页面,每页都有相同的9个项目!

项目列表

  <ul class="thumbnails" >
      {% for dress in all_dress %}
    <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>

1 个答案:

答案 0 :(得分:3)

你应该这样做:

paginator = Paginator(all_dress, 3)
page = request.GET.get('page')

try:
    dresss = paginator.page(page)
except PageNotAnInteger:
    dresss = paginator.page(1)
except EmptyPage:
    dresss = paginator.page(paginator.num_pages)