Skip to content

Commit

Permalink
Add pagination to categories page
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodiegoss committed Jun 22, 2024
1 parent 9e805e4 commit 71b8849
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/blog/jinja2/blog/post_preview.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
</div>
{% endfor %}
{% if posts.count() == page_size %}
<button hx-get="/blog/?page={{ next_page_number }}" hx-trigger="click" hx-swap="outerHTML"> {{_("See more")}}</button>
<button hx-get="{{ page_url }}?page={{ next_page_number }}" hx-trigger="click" hx-swap="outerHTML"> {{_("See more")}}</button>
{% endif %}
17 changes: 13 additions & 4 deletions src/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@ def blog_index(request):
"posts": posts,
"PREVIEW_SIZE": PREVIEW_SIZE,
"page_size": PAGE_SIZE,
"page_url": "/blog/"
}
if request.htmx:
context["next_page_number"] += 1
return render(request, "blog/post_preview.jinja2", context)

return render(request, "blog/index.jinja2", context)


def blog_category(request, category):

def blog_category(request, category):
actual_page_number = int(request.GET.get("page", "1"))
initial_post = 0 + (actual_page_number - 1) * PAGE_SIZE
last_post = PAGE_SIZE * actual_page_number
posts = Post.objects.filter(
categories__name__contains=category
).order_by("-created")
).order_by("-created")[initial_post:last_post]

context = {
"next_page_number": actual_page_number + 1,
"category": category,
"posts": posts,
"PREVIEW_SIZE": PREVIEW_SIZE,
"page_size": PAGE_SIZE,
"page_url": request.path,
}

if request.htmx:
context["next_page_number"] += 1
return render(request, "blog/post_preview.jinja2", context)

return render(request, "blog/category.jinja2", context)

def blog_detail(request, pk):
Expand Down

0 comments on commit 71b8849

Please sign in to comment.