페이징 처리에 좀더 나은 방법이 있을까요? 정보
페이징 처리에 좀더 나은 방법이 있을까요?본문
운영중인 홈피에서 특정 object (상세페이지) 에서 리스트 버튼이나 breadcrumb 를 클릭했을 때
해당 object가 속한 페이지로 이동하도록 했습니다.
한 페이지 당 12개의 object를 가져옵니다.
1. 블로그
블로그 게시물의 경우 Featured 여부순, 작성일 역순으로 불러옵니다.
Featured : article_type 컬럼값이 1
Featured 가 아닌 것 : Article_type 컬럼값이 2
작성일 컬럼 : created_at
코드:
article = get_object_or_404(Article, id=id) af = Article.objects.filter(status='1', article_type__in=['1', '2']) if article.article_type == '1':
xcount = len(af.filter(article_type='1', created_at__gte=article.created_at)) elif article.article_type == '2':
xcount = len(af.filter(article_type='1')) + len(af.filter(article_type=article.article_type, created_at__gte=article.created_at)) page = math.ceil(xcount / 12) if xcount > 0 else 1
2. 책정보
책정보는 점수 역순, 책이름순으로 불러옵니다.
score: 점수
title: 책이름
코드:
book = get_object_or_404(Book, id=id)
limiter = len(Book.objects.all().order_by('-score').filter(score__gt=book.score))
xcount = limiter + len(Book.objects.filter(score=book.score, title__lte=book.title))
page = math.ceil(xcount / 12) if xcount > 0 else 1
페이지는 정상동작하지만 코드를 좀 더 간결하게 만들 수도 있을 것 같아서 글을 남깁니다,
감사합니다!
추천
1
1
댓글 0개