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))