Fakultas Ilmu Komputer UI

Commit 2d742024 authored by Azhar Rais's avatar Azhar Rais
Browse files

[GREEN] Implement pagination on forum home page

parent 5cdf8517
Pipeline #59976 passed with stages
in 12 minutes and 37 seconds
......@@ -31,21 +31,36 @@
{% empty %}
<h4 id="no-discussion">There is no discussion at the moment</h4>
{% endfor %}
<div class="pagination">
<span class="before">
{% if page_obj.has_previous %}
<a href="?page=1">first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
</span>
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="" tabindex="-1">Previous</a>
</li>
{% endif %}
<span class="current">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</span>
<span class="after">
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last</a>
{% for page in pages %}
{% if page == "..." %}
<li class="page-item disabled"><a class="page-link" href="">{{ page }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ page }}">{{ page }}</a></li>
{% endif %}
</span>
</div>
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" tabindex="-1">Next</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="" tabindex="-1">Next</a>
</li>
{% endif %}
</ul>
</nav>
</div>
{% endblock content %}
\ No newline at end of file
......@@ -19,6 +19,36 @@ class ForumHomePage(ListView):
queryset = Discussion.objects.order_by('-updated_at')
template_name = 'forum/forum_home.html'
def pagination(self, current, last):
delta = 2
left = current - delta
right = current + delta + 1
pages = []
pages_with_dots = []
for i in range(last + 1):
if i == 0:
continue
if i == 1 or i == last or (left <= i < right):
pages.append(i)
temp = None
for page in pages:
if temp:
if page - temp == 2:
pages_with_dots.append(temp + 1)
elif page - temp != 1:
pages_with_dots.append("...")
pages_with_dots.append(page)
temp = page
return pages_with_dots
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
page_context = context.get('page_obj')
context['pages'] = self.pagination(page_context.number, page_context.paginator.num_pages)
return context
class ForumCreateDiscussion(LoginRequiredMixin, CreateView):
form_class = DiscussionForm
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment