Skip to content

Commit

Permalink
Added pagination to the post list view
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent 43b4d5f commit 8df1683
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
Binary file modified Module02/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions Module02/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ <h2>
{{ post.body|truncatewords:30|linebreaks }}
<hr />
{% endfor %}
{% include "pagination.html" with page=posts %}
{% endblock %}
13 changes: 13 additions & 0 deletions Module02/mysite/blog/templates/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>
12 changes: 11 additions & 1 deletion Module02/mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def post_list(request):
posts = Post.objects.all()
post_list = Post.objects.all()
paginator = Paginator(post_list, 2)
page_number = request.GET.get('page', 1)

try:
posts = paginator.page(page_number)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post/list.html', {'posts': posts})

def post_detail(request, year, month, day, post):
Expand Down

0 comments on commit 8df1683

Please sign in to comment.