listview의 주요 문제점은 사용하기 어렵고 혼란스러울 수 있다는 것입니다.
with pagination
I am trying to create a listview with pagination in Django. I have tried the following code but it is not working:
<code>class MyListView(ListView):
model = MyModel
template_name = 'my_template.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page'] = self.request.GET.get('page')
return context
def get(self, request, *args, **kwargs):
response = super().get(request, *args, **kwargs)
try:
page = int(response.context['page']) - 1
if page < 0: page = 0 # first page is 1 not 0! (paginator bug?)
response.context['previous'] = str(page) if page > 0 else None # None for first page! (paginator bug?)
response.context['next'] = str(page + 2) if len(response.context['object_list']) == 10 else None # None for last page! (paginator bug?)
except KeyError: pass # no 'page' in the context... means we're on the first one! (no previous!) or last one! (no next!) or something went wrong... just ignore it and don't add anything to the context then...
return response
</code>
이 코드는 페이지 매김이 있는 목록 보기에 대한 클래스 기반 보기입니다. 처음 네 줄은 클래스, 사용할 모델, 사용할 템플릿 및 사용할 컨텍스트 데이터를 정의합니다. 다음 네 줄은 get 메서드와 응답을 정의합니다. 마지막 네 줄은 get_context_data 메서드를 정의하고 컨텍스트 데이터를 반환합니다.
리스트뷰란?
listview는 항목 목록을 표시하는 Django의 위젯입니다.