python - Django Tags results pagination not working as I expected -
i'm trying paginate results tags, it's not working right. if have 3 posts, paginator make have 3 results pages, if have 2 posts, paginator have 2 results pages. how ever many posts have thats how many pages paginator have. paginator works if use return normal results database. did copy , try modify suit tags paginations. it's not working expected. put pagination in includes file, trying keep things dry, , i'm not sure if me issue here's code.
pagination.html:
<div class="text-center" style="margin-bottom: 20px"> <ul class="pagination"> {% if queryset.has_previous %} <li><a href="?page=1"><<</a></li> <li><a href="?{{ page_request_var }}={{ queryset.previous_page_number }} {% if request.get.q %}&q={{ request.get.q }}{% endif %}">prev</a></li> {% endif %} {% in paginator.page_range %} <li {% if page_obj.number == %} class="active" {% endif %}><a href="?page={{i}}">{{i}}</a><li> <!--page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.--> {% endfor %} {% if queryset.has_next %} <li><a href="?{{ page_request_var }}={{ queryset.next_page_number }} {% if request.get.q %}&q={{ request.get.q }}{% endif %}">next</a></li> <li><a href="?page={{ queryset.paginator.num_pages }}">>></a></li> {% endif %} </ul>
this pagination not work how want views.py:
def tag_list(request, slug=none): instance = get_object_or_404(tag, slug=slug) ins = instance.post_set.all() query = request.get.get("q") if query: ins = ins.filter( q(title__icontains=query) | q(content__icontains=query) | q(user__first_name__icontains=query) | q(user__last_name__icontains=query) ).distinct() paginator = paginator(ins, 1) page_request_var = "page" page = request.get.get(page_request_var) try: queryset = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. queryset = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. queryset = paginator.page(paginator.num_pages) context = { "instance": ins, "queryset": queryset, "paginator": paginator, } return render(request, "posts/tag_list.html", context)
the following copied pagination. 1 works
def post_list(request): today = timezone.now().date() queryset_list = post.objects.active() if request.user.is_staff or request.user.is_superuser: queryset_list = post.objects.all() query = request.get.get("q") if query: queryset_list = queryset_list.filter( q(title__icontains=query) | q(content__icontains=query) | q(user__first_name__icontains=query) | q(user__last_name__icontains=query) ).distinct() paginator = paginator(queryset_list, 6) # show 25 contacts per page page_request_var = "page" page = request.get.get(page_request_var) try: queryset = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. queryset = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. queryset = paginator.page(paginator.num_pages) context = { "queryset": queryset, "title": "posts", "page_request_var": page_request_var, "today": today, "queryset_list": queryset_list, "paginator": paginator, } return render(request, "posts/post_list.html", context)
the second argument of paginator how many results show per page, have set one, per comment, want 4
paginator = paginator(ins, 1)
should
paginator = paginator(ins, 4)
you never pass page_request_var
template, means won't ever find correct page, , returns same page
context = { "instance": ins, "queryset": queryset, "paginator": paginator, "page_request_var": page_request_var }
Comments
Post a Comment