2015-05-31 164 views
0

我使用這個代碼到我的網頁上顯示分頁Django分頁 - 如何限制頁面?

  <ul class="pagination pagination-sm"> 
       {% if restaurants.has_previous %} 
        <li><a href="?page={{ restaurants.previous_page_number }}">Prev</a></li> 
       {% endif %} 
       {% for page in restaurants.paginator.page_range %} 
        <li class="{% if restaurants.number == page %}active{% endif %}"><a 
          href="?page={{ page }}">{{ page }}</a></li> 
       {% endfor %} 

       {% if restaurants.has_next %} 
        <li><a href="?page={{ restaurants.next_page_number }}">Next</a></li> 
       {% endif %} 
      </ul> 

的問題是,上面的代碼顯示了所有頁面是這樣的:

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | and so on.. 

相反,我想展示一些東西像這個:

1 | 2 | 3 | 4 | 5 

我該怎麼做?

這是我的視圖:

def listing(request): 
    list_restaurants = ShopAccount.objects.filter(is_active=1) 
    # only active products 
    paginator = Paginator(list_restaurants, 20) # Show 20 products per page 

    page = request.GET.get('page') 
    try: 
     restaurants = paginator.page(page) 
    except PageNotAnInteger: 
     # If page is not an integer, deliver first page. 
     restaurants = paginator.page(1) 
    except EmptyPage: 
     # If page is out of range (e.g. 9999), deliver last page of results. 
     restaurants = paginator.page(paginator.num_pages) 

    return render_to_response('restaurants/list.html', {'restaurants': restaurants}, context_instance=RequestContext(request)) 

回答

0
ShopAccount.objects.filter(is_active=1)[:5] 

試試這個代碼以上,或使用[5]上的分頁程序。

1

我用的是這樣的:

<li {% ifequal page current %}class="active"{% endifequal %}> 
    <a href={% ifequal page -4 %} 
    "/blog/1/">&lt;&lt; 
    {% else %}{% ifequal page -3 %} 
    "/blog/{{ current | add:"-1" }}/">&lt; 
    {% else %}{% ifequal page -2 %} 
    "/blog/{{ current | add:"1" }}/">&gt; 
    {% else %}{% ifequal page -1 %} 
    "/blog/{{ maximum }}/">&gt;&gt; 
    {% else %}{% ifequal page 0 %} 
    "#">... 
    {% else %}{% ifequal page current %} 
    "#" class="page_link">{{ page }} 
    {% else %} 
    "/blog/{{ page }}/">{{ page }} 
    {% endifequal %} 
    {% endifequal %} 
    {% endifequal %} 
    {% endifequal %} 
    {% endifequal %} 
    {% endifequal %} 
    </a> 
</li> 

而這些看法:

def makepaginator(current, maximum): 
    """makepaginator() returns a set of number which reprensents buttons.                                          
    -4 means first page.                                                      
    -3 means previous page.                                                     
    -2 means next page.                                                      
    -1 means last page.                                                      
    0 means a dot.                                                       
    others means the number of the target page.""" 

    NB_PAGES_LEFT_LEFT = 3 
    NB_PAGES_RIGHT_RIGHT = NB_PAGES_LEFT_LEFT 
    NB_PAGES_CENTER_RIGHT = 3 
    NB_PAGES_CENTER_LEFT = NB_PAGES_CENTER_RIGHT 
    i = 2 
    current = int(current) 
    if current > 1: 
    p = [-4, -3] 
    else: 
     p = [] 
    p.append(1) 
    while i <= maximum: 
     if (i > NB_PAGES_LEFT_LEFT and i < maximum - NB_PAGES_RIGHT_RIGHT 
      and (i - current > NB_PAGES_CENTER_RIGHT 
       or current - i > NB_PAGES_CENTER_RIGHT + 1)): 
      p.append(0) 
      while (i > NB_PAGES_LEFT_LEFT 
        and i < maximum - NB_PAGES_RIGHT_RIGHT 
        and (i - current > NB_PAGES_CENTER_RIGHT 
         or current - i > NB_PAGES_CENTER_LEFT + 1)): 
       i += 1 
     else: 
      p.append(i) 
     i += 1 
    if current < maximum: 
     p.append(-2) 
     p.append(-1) 
    return (p) 

def news(request, page_num=1): 
    paginator = Paginator(Article.objects.all().order_by('-date'), settings.ARTICLES_PER_PAGE) 
    try : 
     page = paginator.page(page_num) 
    print(type(page_num)) 
     return (render(request, "main/blog.html", 
         {"paginator" : paginator, "page_num" : int(page_num), 
         "page" : page, 
         "buttons" : makepaginator(page_num, 
                paginator.num_pages)})) 
    except (PageNotAnInteger, EmptyPage) as e: 
     return (notfound(request)) 

自舉我得到這個: pagination

它可能看起來很髒,但我的網站工作。你可以嘗試一下。 如果你有一些改進建議,但我會很高興。 :)