# blog/views.py
from django.shortcuts import render
# Create your views here.
from django.views.generic import ListView, ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, \
TodayArchiveView, DetailView
from blog.models import Post
class PostLV(ListView):
model = Post
template_name = 'blog/post_all.html'
context_object_name = 'posts'
paginate_by = 2
class PostDV(DetailView):
model = Post
class PostAV(ArchiveIndexView):
model = Post
date_field = 'modify_dt'
class PostYAV(YearArchiveView):
model = Post
date_field = 'modify_dt'
make_object_list = True
class PostMAV(MonthArchiveView):
model = Post
date_field = 'modify_dt'
class PostDAV(DayArchiveView):
model = Post
date_field = 'modify_dt'
class PostTAV(TodayArchiveView):
model = Post
date_field = 'modify_dt'
● class PostLV(ListView):
ListView 제네릭 뷰는 테이블로부터 객체리스트를 가져와 그 리스트를 출력합니다.
● template_name = 'blog/post_all.html'
디폴트 템플릿 파일명은 blog/object_list.html이 됩니다.
● context_object_name = 'posts'
템플릿 파일로 넘겨주는 객체 리스트에 대한 컨텍스트 변수명을 posts로 지정합니다. 디폴트 컨텍스트 변수명은 object_list입니다.
● paginate_by = 2
한 페이지에 보여주는 객체 리스트의 숫자는 2입니다. 페이징 기능 구현 가능
● class PostDV(DetailView):
DetailView 제네릭 뷰는 테이블로부터 특정 객체를 가져와 그 객체의 상세정보를 출력합니다. 테이블에서 특정 객체를 가져올 때 기본키 대신 슬러그를 사용하기 때문에 slug 파라미터는 URLconf에서 추출해 뷰로 넘겨줍니다.
● class PostAV(ArchiveIndexView):
ArchiveIndexView 제네릭 뷰는 테이블로부터 날짜 필드의 연도를 기준으로 객체 리스트를 가져와 그 객체들이 속한 월을 리스트로 출력합니다.
● date_field = 'modify_dt'
기준이 되는 날짜 필드는 modify_dt입니다.
● make_object_list = True
make_object_list=True 이므로 해당 연도에 해당하는 객체의 리스트를 만들어서 템플릿에 넘겨줍니다. 즉 템플릿 파일에서 object_list 컨텍스트 변수를 사용할 수 있습니다.
'Django' 카테고리의 다른 글
[Django] heroku 서버 배포 (1) | 2022.03.03 |
---|---|
[BlogApp] 태그 기능 개발 (0) | 2022.01.24 |
[BlogApp] URLconf 2계층으로 코딩하기 (0) | 2022.01.24 |
[BlogApp] admin.py 작성 (0) | 2022.01.23 |
[BookmarkApp] admin.py 작성 (1) | 2022.01.23 |