官术网_书友最值得收藏!

  • Django 2 by Example
  • Antonio Melé
  • 427字
  • 2021-06-25 21:19:03

Adding pagination

When you start adding content to your blog, you will soon realize you need to split the list of posts across several pages. Django has a built-in pagination class that allows you to manage paginated data easily.

Edit the views.py file of the blog application to import the Django paginator classes and modify the post_list view, as follows:

from django.core.paginator import Paginator, EmptyPage,\
PageNotAnInteger

def post_list(request):
object_list = Post.published.all()
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts})

This is how pagination works:

  1. We instantiate the Paginator class with the number of objects we want to display on each page.
  2. We get the page GET parameter that indicates the current page number.
  3. We obtain the objects for the desired page calling the page() method of Paginator.
  4. If the page parameter is not an integer, we retrieve the first page of results. If this parameter is a number higher than the last page of results, we will retrieve the last page.
  5. We pass the page number and retrieved objects to the template.

Now, we have to create a template to display the paginator so that it can be included in any template that uses pagination. In the templates/ folder of the blog application, create a new file and name it pagination.html. Add the following HTML code to the file:

<p class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</p>

The pagination template expects a Page object in order to render previous and next links and to display the current page and total pages of results. Let's return to the blog/post/list.html template and include the pagination.html template at the bottom of the {% content %} block, as follows:

{% block content %}
...
{% include "pagination.html" with page=posts %}
{% endblock %}

Since the Page object we are passing to the template is called posts, we include the pagination template in the post list template, passing the parameters to render it correctly. You can follow this method to reuse your pagination template in paginated views of different models.

Now, open http://127.0.0.1:8000/blog/ in your browser. You should see the pagination at the bottom of the post list and should be able to navigate through pages:

主站蜘蛛池模板: 张掖市| 于都县| 旌德县| 兴和县| 胶南市| 蕲春县| 洛阳市| 剑河县| 夹江县| 梅州市| 丰都县| 洱源县| 安化县| 武义县| 明光市| 嫩江县| 屯门区| 五指山市| 建瓯市| 望城县| 泽库县| 塔河县| 琼海市| 浙江省| 达尔| 砀山县| 息烽县| 民勤县| 公主岭市| 泽库县| 宝清县| 安化县| 盐源县| 沁水县| 闽清县| 永顺县| 吉木乃县| 磐安县| 都昌县| 筠连县| 隆德县|