I have following view and the content is rendered correctly. The pagination is also correct. The only issue is that the page_obj.number in the template is always 1. Even clicking on page 3 the content of page 3 is shown but the page_obj.number is still 1. Maybe I'm doing something wrong in my view. Here the view:
class SpecificationListView(AjaxRequiredMixin, ListView):
model = Category
context_object_name = 'categories'
template_name = 'catalog/specification/listSpecificationFiltered.html'
paginate_by = 2
def get_queryset(self):
search_value = self.request.POST.get('searchValue', None)
sel_category = self.request.POST.get('selCategory', 'ALL')
chk_active = self.request.POST.get('chkActive', 0)
category_filters = Q()
if sel_category != 'ALL':
category_filters = category_filters & Q(pk=sel_category)
specification_filters = Q()
search_value = None if (search_value == '' or len(search_value) < 3) else search_value
if search_value is not None:
specification_filters = specification_filters & Q(name__icontains=search_value)
if chk_active == '1':
specification_filters = specification_filters & Q(is_active=True)
# categories are retrieved to build the tree in template
categories_list = Category.objects.distinct().filter(category_filters)
category_ids = categories_list.values_list('id', flat=True) # category ids on page
specification_filters = specification_filters & Q(category_id__in=category_ids)
categories_list = categories_list.prefetch_related(
Prefetch(
'specifications',
queryset=Specification.objects.filter(specification_filters)
)
)
return categories_list
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(SpecificationListView, self).get_context_data(**kwargs)
page = self.request.POST.get('page')
paginator = Paginator(self.object_list, self.paginate_by)
categories = paginator.get_page(int(page))
context.update({
'categories': categories,
'page_title': _('Manage Specifications'),
'actions_allowed': self.request.user.is_allowed('Specification.Manage'),
})
return context
In my template I use a templatetag to render the pagination like:
{% if is_paginated %}
{{ page_obj|render_paginator }}
{% endif %}
The templatetag looks like this:
@register.filter(name='render_paginator')
def paginate_(page_obj):
html = '<div class="d-flex justify-content-between align-items-center flex-wrap">'
html += '<div class="d-flex flex-wrap py-2 mr-3">'
previous_page = (page_obj.number - 1) if (page_obj.number - 1) > 0 else 1
last_page = len(page_obj.paginator.page_range)
next_page = (page_obj.number + 1) if (page_obj.number + 1) <= last_page else last_page
disabled = ''
if page_obj.number == 1:
disabled = ' disabled'
html += '<a href="?page=1" class="btn btn-icon btn-sm btn-light mr-2 my-1{}"><i class="ki ki-bold-double-arrow-back icon-xs"></i></a>'.format(disabled)
html += '<a href="?page={}" class="btn btn-icon btn-sm btn-light mr-2 my-1{}"><i class="ki ki-bold-arrow-back icon-xs"></i></a>'.format(page_obj.previous_page_number, disabled)
for nb in page_obj.paginator.page_range:
active = ''
if page_obj.number == nb:
active = 'btn-hover-primary active'
html += '<a href="?page={}" class="btn btn-icon btn-sm border-0 btn-light {} mr-2 my-1">{}</a>'.format(nb, active, nb)
disabled = ' disabled'
if page_obj.has_next:
disabled = ''
html += '<a href="?page={}" class="btn btn-icon btn-sm btn-light mr-2 my-1{}"><i class="ki ki-bold-arrow-next icon-xs"></i></a>'.format(page_obj.next_page_number, disabled)
html += '<a href="?page={}" class="btn btn-icon btn-sm btn-light mr-2 my-1{}"><i class="ki ki-bold-double-arrow-next icon-xs"></i></a>'.format(page_obj.paginator.num_pages, disabled)
html += '</div>'
html += '<div class="d-flex align-items-center py-3">'
html += '<select id="selLimit" class="form-control form-control-sm font-weight-bold mr-4 border-0 bg-light" style="width: 75px;">'
for i in range(5, 50, 5):
html += '<option value="{}">{}</option>'.format(i, i)
html += '</select>'
html += '</div>'
html += '</div>'
return mark_safe(html)
Thank you in advance for any help