-
한 뷰 안에 여러개의 페이지네이션 구현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 %}
'Django' 카테고리의 다른 글
Django ajax 좋아요 구현 (0) 2021.01.22 youtube API를 통해 동영상 검색하고, 검색 결과 TinyMCE 삽입하기 (0) 2021.01.22 [error] 첨부파일 에러 (0) 2021.01.22 TinyMCE 사진 넣기/업로드 (0) 2021.01.21