Answer the question
In order to leave comments, you need to log in
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})
{% if users.has_other_pages %}
<ul class="pagination">
{% if users.has_previous %}
<li><a href="?page={{ users.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</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 }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
<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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question