Pagination within Django - combining two projects
I am working off of two different projects. I have the Example By 5 and a YT series that I previously used to get started with Django. I liked the Pagination from the yt series so I decided to use that method, however in the Example By process, we moved from the Class base view to a function and now the pagination does not show up. I have added what I thought was needed in the view and html page but still getting nothing.
VIEW code:
def post_list(request, tag_slug=None):
post_list = Post.published.all()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
post_list = post_list.filter(tags__in=[tag])
paginator = Paginator(post_list, 3) # 3 posts in each page
page_number = request.GET.get('page') # Get the page number
page_obj = paginator.get_page(page_number)
context = {'page_obj': page_obj,'is_paginated': page_obj.has_other_pages()} # Check if pagination is needed
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, 'context' : context, 'tag': tag})
Here is the Pagination section of the html:
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}