如何在基于 Django 函数的视图中使用 paginate_orphans?
How to use paginate_orphans in django function based views?
在基于Class的视图中我们可以这样使用。
class PostListView(ListView):
model = Post
template_name ='blog/index.html'
ordering = ['id']
paginate_by = 5
paginate_orphans = 2
但是如何在基于函数的视图中使用此 paginate_orphans
?
page = request.GET.get('page', 1)
paginator = Paginator(filter_qs, 2)
try:
filter_qs = paginator.page(page)
except PageNotAnInteger:
filter_qs = paginator.page(1)
except EmptyPage:
filter_qs = paginator.page(paginator.num_pages)
您可以将 orphans 参数传递给 Paginator class,如下所述:
paginator = Paginator(filter_qs, 2, orphans=2)
在基于Class的视图中我们可以这样使用。
class PostListView(ListView):
model = Post
template_name ='blog/index.html'
ordering = ['id']
paginate_by = 5
paginate_orphans = 2
但是如何在基于函数的视图中使用此 paginate_orphans
?
page = request.GET.get('page', 1)
paginator = Paginator(filter_qs, 2)
try:
filter_qs = paginator.page(page)
except PageNotAnInteger:
filter_qs = paginator.page(1)
except EmptyPage:
filter_qs = paginator.page(paginator.num_pages)
您可以将 orphans 参数传递给 Paginator class,如下所述:
paginator = Paginator(filter_qs, 2, orphans=2)