Django Tags results pagination not working as I expected
<p>I'm trying to paginate the results I get back from my tags, but it's not working right. If I have three posts, the paginator will make have three results pages, if I have two posts, the paginator will have two results pages. How ever many posts I have Thats how many pages the paginator will have. The paginator works if I use it to return normal results from the database. What I did was copy it and try to modify it to suit the tags paginations. But it's not working as I expected. I also put the pagination in an includes file, trying to keep things DRY, and I'm not sure if that me be the issue here's my code.</p>

<p>pagination.html:</p>

<pre><code>&lt;div class="text-center" style="margin-bottom: 20px"&gt;
&lt;ul class="pagination"&gt;
{% if queryset.has_previous %}
    &lt;li&gt;&lt;a href="?page=1"&gt;&lt;&lt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="?{{ page_request_var }}={{ queryset.previous_page_number }}
{% if request.GET.q %}&amp;q={{ request.GET.q }}{% endif %}"&gt;prev&lt;/a&gt;&lt;/li&gt;
{% endif %}


{% for i in paginator.page_range %}

 &lt;li {% if page_obj.number == i %} class="active" {% endif %}&gt;&lt;a href="?page={{i}}"&gt;{{i}}&lt;/a&gt;&lt;li&gt;
&lt;!--Page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.--&gt;

{% endfor %}


{% if queryset.has_next %}
    &lt;li&gt;&lt;a href="?{{ page_request_var }}={{ queryset.next_page_number }}
    {% if request.GET.q %}&amp;q={{ request.GET.q }}{% endif %}"&gt;next&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="?page={{ queryset.paginator.num_pages }}"&gt;&gt;&gt;&lt;/a&gt;&lt;/li&gt;
{% endif %}
&lt;/ul&gt;
</code></pre>

<p></p>

<p>this pagination does not work how I want it to views.py:</p>

<pre><code>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 is not an integer, deliver first page.
    queryset = paginator.page(1)
except EmptyPage:
    # If page is 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)
</code></pre>

<p>the following is where I copied pagination. This one works</p>

<pre><code>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 is not an integer, deliver first page.
    queryset = paginator.page(1)
except EmptyPage:
    # If page is 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)
</code></pre>