O

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

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

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

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

자유게시판

+
제목 글쓴이 날짜 조회
3년 전 조회 2,047
3년 전 조회 1,657
3년 전 조회 1,780
3년 전 조회 1,184
3년 전 조회 1,942
3년 전 조회 1,419
3년 전 조회 1,221
3년 전 조회 1,403
3년 전 조회 1,459
3년 전 조회 1,261
3년 전 조회 2,151
3년 전 조회 1,794
3년 전 조회 1,490
3년 전 조회 1,331
3년 전 조회 1,182
3년 전 조회 1,774
3년 전 조회 1,966
3년 전 조회 1,489
3년 전 조회 1,190
3년 전 조회 2,150
3년 전 조회 1,758
3년 전 조회 1,480
3년 전 조회 1,450
3년 전 조회 968
3년 전 조회 1,353
3년 전 조회 1,251
3년 전 조회 1,303
3년 전 조회 1,201
3년 전 조회 1,606
3년 전 조회 1,920