0

Hi i want to paginating queryset(lectures). and i tried. but it doesn'work how can i do?

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
           })

1 Answers1

2

Just make it a ListView and you won't have to worry about how it all works since paginate_by sets up pagination for you

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'])
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Thank you very much But i can paginating by ListView. I want to use 'View' there are other way to solve it? – Jung Yoon Kim Dec 23 '16 at 08:02
  • @JungYoonKim - I don't know, you haven't told us *what* error you get. Although if list view works, what do you need a `View` for? – Sayse Dec 23 '16 at 08:03
  • Actually i work with my coworker and he run development server and he does not response now. so I don't know what error i get. sorry sorry. I edit code to full code. There are any way to convert my View to ListView? – Jung Yoon Kim Dec 23 '16 at 08:12
  • @JungYoonKim - You mean you aren't able to run a local development copy? There isn't any logging set up to output these errors somewhere? You're not able to change the code at all anyway?... – Sayse Dec 23 '16 at 08:13