您好我想分页查询集(讲座)。我试过但它不起作用 我能怎么做?
class tag_detail(View): def get(self, request, pk): tag_hit = get_object_or_404(TagModel, id=pk) tag_hit.view_cnt = tag_hit.view_cnt + 1 tag_hit.save() tag = TagModel.objects.get(id=pk) lectures_data = LectureModel.objects.filter(tags__id=pk).order_by('-id') paginator = Paginator(lectures_data, 2) page = request.GET.get('page') try: lectures = paginator.page(page) except PageNotAnInteger: lectures = paginator.page(1) except EmptyPage: lectures = paginator.page(paginator.num_pages) return render(request, 'web/html/tag/tag_detail.html',{ 'lectures':lectures 'tag':tag })
答案 0 :(得分:2)
只需将其设为ListView
,您就不必担心这一切是如何运作的,因为paginate_by
为您设置了分页
class tag_detail(ListView): # TagDetailListView would be a better name
paginate_by = 2
template_name = 'web/html/tag/tag_detail.html'
model = LectureModel
ordering = '-id'
context_object_name = 'lectures'
def get_queryset(self):
return LectureModel.objects.filter(tags__id=self.kwargs['pk'])