Created
April 19, 2012 08:17
-
-
Save stefanocrosta/2419590 to your computer and use it in GitHub Desktop.
Django Paginated Class View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Very simple PaginatedListView | |
""" | |
class PaginatedListView(ListView, FormView): | |
queryset = None | |
def get_context_data(self, **kwargs): | |
#logging.debug('ApplicationListView.get_context_data') | |
context = super(self.__class__, self).get_context_data(**kwargs) | |
# paginate by | |
context['is_paginated'] = self.is_paginated | |
context['page_obj'] = self.page_obj | |
return context | |
def get_queryset(self): | |
## this is where you put your code ## | |
query_set = CALCULATE YOUR QUERYSET HERE. or use self.query_set | |
## end of your code ## | |
# make my own (simple?) pagination | |
count = query_set.count() | |
MAX_ELEMENTS = 20 | |
try: | |
page = int(self.request.GET.get('page', 0)) | |
except: | |
page = 0 | |
if page or count>MAX_ELEMENTS: | |
import math | |
if not page: | |
page = 1 | |
self.is_paginated = True | |
_from = (page-1)*MAX_ELEMENTS | |
_to = min(page*MAX_ELEMENTS-1, count) | |
last_page = math.trunc(count / MAX_ELEMENTS)+1 | |
query_set = query_set[_from:_to] | |
query_string = self.request.GET.copy() | |
query_string['page'] = page+1 | |
next_page_url = query_string.urlencode() | |
query_string['page'] = page-1 | |
prev_page_url = query_string.urlencode() | |
self.page_obj = { | |
'has_previous': page>1, | |
'has_next': page<last_page, | |
'number': page, | |
'next_page_number': page+1, | |
'next_page': next_page_url, | |
'prev_page_number': page-1, | |
'prev_page': prev_page_url, | |
'paginator': { | |
'num_pages': last_page | |
} | |
} | |
else: | |
self.is_paginated = False | |
self.page_obj = {} | |
return query_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are still writing with the old style views (booo!) you need to:
def get_queryset(self):
and make it intodef get_paginated_queryset(request, queryset):
self.
with... nothingyourqueryset, is_paginated, page_obj = get_paginated_queryset(request, yourqueryset)
return render(request, 'myapp/template.html', {"queryset": yourqueryset, "is_paginated": is_paginated, "page_obj": page_obj})