[django] python django 게시판 만들기 - Pagination
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
[django] python django 게시판 만들기 - Pagination
[django] python django 게시판 만들기 - Pagination
홈에 나오는 게시물들을 자른다!
-----------------------------------------------------------------------------------------------------------------------------------vscode 설치하기 : https://integer-ji.tistory.com/65
python 설치하기 : https://integer-ji.tistory.com/64
git 설치하기 : https://integer-ji.tistory.com/66
vscode 설정하기 : https://integer-ji.tistory.com/81
hello world 띄우기 : https://integer-ji.tistory.com/82
git 초기 설정 : https://integer-ji.tistory.com/83
page 이동 : https://integer-ji.tistory.com/84
word count 실습 ( 1 ) : https://integer-ji.tistory.com/85
word count 실습 ( 2 ) : https://integer-ji.tistory.com/86
python django 게시판 만들기 ( 1 ) : https://integer-ji.tistory.com/89
python django 게시판 만들기 ( 2 ) : https://integer-ji.tistory.com/90
python django 게시판 만들기 ( 3 ) : https://integer-ji.tistory.com/91
python django 게시판 만들기 ( 4 ) : https://integer-ji.tistory.com/93
python django 게시판 만들기 ( 5 ) : https://integer-ji.tistory.com/94
python django 게시판 만들기 ( 6 ) : https://integer-ji.tistory.com/95
python django 게시판 만들기 ( 7 ) : https://integer-ji.tistory.com/97
python django 게시판 만들기 ( 8 ) : https://integer-ji.tistory.com/99
python django 게시판 만들기 ( 9 ) : https://integer-ji.tistory.com/100
python django 게시판 만들기 ( 10 ) : https://integer-ji.tistory.com/101
python django 게시판 만들기 ( 11 ) : https://integer-ji.tistory.com/102
-----------------------------------------------------------------------------------------------------------------------------------
home 함수 수정
def home(request): blogs = Blog.objects.order_by('-id') blog_list = Blog.objects.all().order_by('-id') paginator = Paginator(blog_list,3) page = request.GET.get('page') posts = paginator.get_page(page) return render(request,'home.html', {'blogs':blogs,'posts':posts} )
paginator을 import 해줍니다.
home 함수를 변경할 건데 지금은 그냥 Blog의 객체 전부를 홈에 나타내 줍니다.
블로그 객체들을 blog_list에 새로 담고 paginator이란 변수에 Paginator를 이용해 3개씩 자른 blog_list를 넣어줍니다.
잘린 3개씩 들어있는 blog_list를 page에 넣어줍니다.
page를 출력하기 위해 posts에 넣어줍니다.
home.html 수정
{% extends 'base.html' %} {% block content %} {% for blog in posts %} {{ blog.title }} {{ blog.pub_date | date:"Y-m-d" }} {{ blog.summary }} ...자세히 보기 {% endfor %} {%if posts.has_previous%} First Previous {%endif%} {{posts.number}} of {{posts.paginator.num_pages}} {%if posts.has_next%} Next Last {%endif%} {% endblock %}
for blog in posts가 수정되었습니다.
블로그에 있는 모든 글을 가져오는 것이 아니라
3개로 잘린 page를 가져와야 합니다. posts로 변경
post.has_previous가 추가되었습니다.
만약 이전 페이지가 존재하지 않으면 나 타지 않습니다.
그리고 현재 페이지 번호와 총페이지를 나타내 줍니다.
다음은 다음 페이지를 나타내 줍니다. 마찬가지로 다음 페이지가 없으면 출력되지 않습니다.
확인
첫 페이지는 이전으로 갈 페이지가 없으니 if문이 실행되지 않습니다.
12페이지 중 2번째 페이지입니다.
마지막 페이지도 무사히 나옵니다.
git push
git init git add . git commit -m "코멘트"
git push origin master
---
빠르게 프로젝트에 진입하기 위해 진도부터 확 빼기
from http://integer-ji.tistory.com/106 by ccl(A)
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
댓글
댓글 쓰기