R
R
rmfalx2017-07-24 15:26:57
Django
rmfalx, 2017-07-24 15:26:57

How to display pagination in Django?

I can't display pagination in my Django project.
The site was set up according to the instructions of Django girls
Read this and this
In the file views.py

from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404
from .forms import PostForm
from django.shortcuts import redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.

def post_list(request):
    posts = Post.objects.filter(created_date__lte=timezone.now()).order_by('-created_date')
    
    paginator = Paginator(posts, 10)

    page = request.GET.get('page')
    try:
        post = paginator.page(page)
    except PageNotAnInteger:

        post = paginator.page(1)
    except EmptyPage:

        post = paginator.page(paginator.num_pages)

    return render(request, 'autist/post_list.html', {'posts': post})

in post_list.html
{% if users.has_other_pages %}
  <ul class="pagination">
    {% if users.has_previous %}
      <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in users.paginator.page_range %}
      {% if users.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if users.has_next %}
      <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

or
<div class="pagination">
    <span class="step-links">
        {% if post.has_previous %}
            <a href="?page={{ post.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ post.number }} of {{ post.paginator.num_pages }}.
        </span>

        {% if post.has_next %}
            <a href="?page={{ post.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

But nothing is displayed. Although the number of displayed pages changes if I change paginator = Paginator(posts, 10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rmfalx, 2017-07-24
@rmfalx

Sorry - this is my own inattention. in post_list.html instead of post - I had to write posts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question