分页
在网站的开发当中,我们常常需要将各种列表中的数据分页显示,想要实现分页的效果,就可以通过django.core.Paginator.Paginator和django.core.Paginator.Page来实现。
1. 比如,将Paginator_pageAPP中表article中的range(1,100)的数据分页显示,每页显示10条数据,可以实现手动分页的效果。
(1)首先在models.py文件中定义模型Article,示例代码如下:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.CharField(max_length=100)
create_time = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'paginator_page'
(2)在views.py文件中为数据库中的表paginator_page添加1-99条数据,示例代码如下:
from django.http import HttpResponse
from .models import Article
def index(request):
articles = []
for page in range(1,100):
article = Article(title='标题:{}'.format(page), content='内容:{}'.format(page))
articles.append(article)
Article.objects.bulk_create(articles)
return HttpResponse('success')
(3)将所有的数据分页显示。这就涉及到了我们之前提到的ListView类,就需要通过ListView来新创建一个类视图,实现我们的分页效果。views.py文件中示例代码如下:
from django.views.generic from ListView
from django.http import HttpResponse
class Paginator_page_view(ListView):
model = Article
template_name = 'paginator_page/static/paginator_page.html'
context_object_name = 'articles'
paginate_by = 10
ordering = 'create_time'
page_kwarg = 'p'
def get_context_data(self, request, *args, **kwargs):
context = super(Paginator_page_view, self).get_context_data(**kwargs)
return context
def get_queryset(self):
return Article.objects.filter(id__lte=80)
(4)在项目的urls.py文件中进行视图函数与url之间的映射,示例代码如下:
from . import views
from django.urls import path, include
urlpatterns = [
path('paginator/', include('Paginator_page.urls')),
]
(5)在Paginator_pageAPP中的urls.py文件中进行视图与url的映射,示例代码如下:
from . import views
from .views import Paginator_page_view
from django.urls import path
app_name = 'Paginator_page'
urlpatterns = [
path('', Paginator_page_view.as_view(), name='page'),
]
(6)在paginator_page/static/paginator_page.html中实现分页(这种情况下会显示每一页,这种不是太方便。接下来会进行完善),示例代码如下:
{% for article in articles %}
{{% article.title %}}
{% endfor %}
(7)完善在paginator_page/static/paginator_page02.html中实现显示部分分页,示例代码如下:
from django.views.generic import ListView
from django.http import HttpResponse
from .models import ListView
class Paginator_page_view02(ListView):
model = Article
template_name = 'paginator_page/static/paginator_page02.html'
context_object_name = 'articles'
paginate_by = 10
ordering = 'create_time'
page_kwarg = 'p'
def get_context_data(self, request, *args, **kwargs):
context = super(Paginator_page_view02, self).get_context_data(**kwargs)
paginator = context.get('paginator')
page_obj = context.get('page_obj')
pagination_data = self.get_pagination_data(paginator, page_obj)
context.update(pagination_data)
return context
def get_pagination_data(self, paginator,page_obj, around_count=2):
current_page = page_obj.number
num_pages = paginator.num_pages
left_has_more = False
right_has_more = False
if current_page <= around_count + 2:
left_pages = range(1, current_page)
else:
lefT_has_more = True
left_pages = range(current_page-around_count, current_page)
if current_page >= num_pages-around_count - 1:
right_page = range(current_page, num_pages)
else:
right_has_more = True
right_page = range(current_page+1, current_page+around_count+1)
return context={
'left_pages': left_pages,
'right_pages': right_pages,
'num_pages': num_pages,
'left_has_more': left_has_more,
'right_has_more': right_has_more,
'current_page': current_page,
}
(8)paginator_page/static/paginator_page02.html中示例代码如下:
{# 上一页 #}
{% if page_obj.has_previous %}
上一页
{% else %}
上一页
{% endif %}
{% if left_has_more %}
1
...
{% endif %}
{# 中间页 #}
{# left页 #}
{% for left_page in left_pages %}
{{ left_page }}
{% endfor %}
{# 中间页 #}
{{ current_page }}
{# right页 #}
{% for right_page in right_pages %}
{{ right_page }}
{% endfor %}
{% if right_has_more %}
...
{{ num_pages }}
{% endif %}
{# 下一页 #}
{% if page_obj.has_next %}
下一页
{% else %}
下一页
{% endif %}