O

페이징 처리에 좀더 나은 방법이 있을까요?

· 2022-03-30 (수) 11:49:38 · 1384

운영중인 홈피에서 특정 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

페이지는 정상동작하지만 코드를 좀 더 간결하게 만들 수도 있을 것 같아서 글을 남깁니다,
감사합니다!

댓글을 작성하시려면 로그인이 필요합니다.

자유게시판

203,421건
+
제목 글쓴이 날짜 조회
22-03-30 조회 1,385
22-03-30 조회 2,028
22-03-29 조회 2,235
22-03-29 조회 1,747
22-03-29 조회 1,381
22-03-29 조회 2,415
22-03-29 조회 2,013
22-03-29 조회 1,704
22-03-29 조회 1,691
22-03-29 조회 1,160
22-03-29 조회 1,564
22-03-29 조회 1,479
22-03-29 조회 1,494
22-03-29 조회 1,404
22-03-29 조회 1,840
22-03-29 조회 2,161
22-03-28 조회 2,517
22-03-28 조회 1,519
22-03-28 조회 1,397
22-03-28 조회 1,771