ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 한 뷰 안에 여러개의 페이지네이션 구현
    Django 2021. 1. 22. 13:57

    참고

     

    http://www.shanegibney.com/shanegibney/django-multiple-pagination-in-a-single-template/

     

     

     

     

     

    view.py

     

    class RecipeLV(ListView):
        context_object_name = 'recipe_list'
    
        def get(self, request, *args, **kwargs):
    
            queryset = RecipeContent.objects.all()
    
            queryset2 = YoutubeContent.objects.all()
    
            paginator = Paginator(queryset, 3)
    
            page = request.GET.get('page1', 1)
    
            try:
                rooms = paginator.page(page)
            except PageNotAnInteger:
                rooms = paginator.page(1)
            except EmptyPage:
                rooms = paginator.page(paginator.num_pages)
    
    
            page = request.GET.get('page2', 1)
            paginator2 = Paginator(queryset2, 3)
    
            try:
                rooms2 = paginator2.page(page)
            except PageNotAnInteger:
                rooms2 = paginator2.page(1)
            except EmptyPage:
                rooms2 = paginator2.page(paginator.num_pages)
    
            sort = request.GET.get('sort', '')
    
            if sort == 'Rec_conPickCount':
                queryset = queryset.order_by('-Rec_conPickCount', '-Rec_conModify')
                paginator = Paginator(queryset, 3)
                rooms = paginator.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
            elif sort == 'Rec_conReadcount':
                queryset = queryset.order_by('-Rec_conReadcount', '-Rec_conModify')
                paginator = Paginator(queryset, 3)
                rooms = paginator.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
            elif sort == 'Rec_conModify':
                queryset = queryset.order_by('-Rec_conModify')
                paginator = Paginator(queryset, 3)
                rooms = paginator.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
    
            elif sort == 'You_conPickCount':
                queryset2 = queryset2.order_by('-You_conPickCount', '-You_conModify')
                paginator2 = Paginator(queryset2, 3)
                rooms2 = paginator2.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
            elif sort == 'You_conReadcount':
                queryset2 = queryset2.order_by('-You_conReadcount', '-You_conModify')
                paginator2 = Paginator(queryset2, 3)
                rooms2 = paginator2.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
            else:
                queryset2 = queryset2.order_by('-You_conModify')
                paginator2 = Paginator(queryset2, 3)
                rooms2 = paginator2.get_page(page)
                return render(request, 'recipe/recipe_list.html', {'recipe_list': queryset, 'youtube_list' : queryset2, 'rooms' : rooms, 'rooms2' : rooms2})
     


     
    recipe_list.html

    {% if rooms.has_previous %}
    <a href="?page1={{rooms.number|add:-1}}">Previous</a>
    {% endif %}
    {% if rooms.has_next %}
    <a href="?page1={{rooms.number|add:+1}}">NEXT</a>
    {% endif %}{% if rooms2.has_previous %}
    <a href="?page2={{rooms2.number|add:-1}}">Previous</a>
    {% endif %}>{% if rooms2.has_next %}
    <a href="?page2={{rooms2.number|add:+1}}">NEXT</a>
    {% endif %} 

    댓글

Designed by Tistory.