views 代码:
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
columns = Column.objects.all()
articles = Article.objects.all()
def index(request):
cus_list = Article.objects.all()
paginator = Paginator(cus_list, 1)
page = request.GET.get('page')
if page:
article_list = paginator.page(page).object_list
else:
article_list = paginator.page(1).object_list
try:
customer = paginator.page(page)
except PageNotAnInteger:
customer = paginator.page(1)
except EmptyPage:
customer = paginator.page(paginator.num_pages)
return render(request, 'index.html', {'cus_list': customer, 'columns': columns, 'articles': article_list})
html模板:
<nav aria-label="Page navigation">
<ul class="pagination">
{% if cus_list.has_previous %}
<li>
<a href="?page={{ cus_list.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">«</span></a>
</li>
{% endif %}
{% for pg in cus_list.paginator.page_range %}
{% if cus_list.number == pg %}
<li class="active"><a href="?page={{ pg }}">{{ pg }}</a></li>
{% else %}
<li><a href="?page={{ pg }}">{{ pg }}</a></li>
{% endif %}
{% endfor %}
{% if cus_list.has_next %}
<li>
<a href="?page={{ cus_list.next_page_number }}" aria-label="Next"><span aria-hidden="true">»</span></a></li>
{% endif %}
</ul>
</nav>
app下面的urls:
url(r'^$', views.index, name='index'),